|
| 1 | +package adminconsole |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "bytes" |
| 6 | + "compress/gzip" |
| 7 | + "context" |
| 8 | + "debug/elf" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "runtime" |
| 13 | + |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + "k8s.io/apimachinery/pkg/api/errors" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | +) |
| 20 | + |
| 21 | +// AdminConsoleCustomization is a struct that contains the actions to create and update |
| 22 | +// the admin console customization found inside the binary. This is necessary for |
| 23 | +// backwards compatibility with older versions of helmvm. |
| 24 | +type AdminConsoleCustomization struct { |
| 25 | + kubeclient client.Client |
| 26 | +} |
| 27 | + |
| 28 | +// extractCustomization will extract the customization from the binary if it exists. |
| 29 | +// If it does not exist, it will return nil, nil. The customization is expected to |
| 30 | +// be found in the sec_bundle section of the binary. |
| 31 | +func (a *AdminConsoleCustomization) extractCustomization() ([]byte, error) { |
| 32 | + exe, err := os.Executable() |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + fpbin, err := elf.Open(exe) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + defer fpbin.Close() |
| 41 | + section := fpbin.Section("sec_bundle") |
| 42 | + if section == nil { |
| 43 | + return nil, nil |
| 44 | + } |
| 45 | + return a.processSection(section) |
| 46 | +} |
| 47 | + |
| 48 | +// processSection searches the provided elf section for a gzip compressed tar archive. |
| 49 | +// If it finds one, it will extract the contents and return the kots.io Application |
| 50 | +// object as a byte slice. |
| 51 | +func (a *AdminConsoleCustomization) processSection(section *elf.Section) ([]byte, error) { |
| 52 | + gzr, err := gzip.NewReader(section.Open()) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + defer gzr.Close() |
| 57 | + tr := tar.NewReader(gzr) |
| 58 | + for { |
| 59 | + header, err := tr.Next() |
| 60 | + switch { |
| 61 | + case err == io.EOF: |
| 62 | + return nil, nil |
| 63 | + case err != nil: |
| 64 | + return nil, fmt.Errorf("unable to read tgz file: %w", err) |
| 65 | + case header == nil: |
| 66 | + continue |
| 67 | + } |
| 68 | + if header.Typeflag != tar.TypeReg { |
| 69 | + continue |
| 70 | + } |
| 71 | + content := bytes.NewBuffer(nil) |
| 72 | + if _, err := io.Copy(content, tr); err != nil { |
| 73 | + return nil, fmt.Errorf("unable to copy file out of tar: %w", err) |
| 74 | + } |
| 75 | + if !bytes.Contains(content.Bytes(), []byte("apiVersion: kots.io/v1beta1")) { |
| 76 | + continue |
| 77 | + } |
| 78 | + if !bytes.Contains(content.Bytes(), []byte("kind: Application")) { |
| 79 | + continue |
| 80 | + } |
| 81 | + return content.Bytes(), nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// apply will attempt to read the helmvm binary and extract the kotsadm portal |
| 86 | +// customization from it. If it finds one, it will apply it to the cluster. |
| 87 | +func (a *AdminConsoleCustomization) apply(ctx context.Context) error { |
| 88 | + logrus.Infof("Applying admin console customization") |
| 89 | + if runtime.GOOS != "linux" { |
| 90 | + logrus.Infof("Skipping admin console customization on %s", runtime.GOOS) |
| 91 | + return nil |
| 92 | + } |
| 93 | + cust, err := a.extractCustomization() |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("unable to extract customization from binary: %w", err) |
| 96 | + } |
| 97 | + if cust == nil { |
| 98 | + logrus.Infof("No admin console customization found") |
| 99 | + return nil |
| 100 | + } |
| 101 | + logrus.Infof("Admin console customization found") |
| 102 | + nsn := client.ObjectKey{Namespace: "helmvm", Name: "kotsadm-application-metadata"} |
| 103 | + var cm corev1.ConfigMap |
| 104 | + if err := a.kubeclient.Get(ctx, nsn, &cm); err != nil { |
| 105 | + if !errors.IsNotFound(err) { |
| 106 | + return fmt.Errorf("unable to get kotsadm-application configmap: %w", err) |
| 107 | + } |
| 108 | + logrus.Infof("Creating admin console customization config map") |
| 109 | + cm = corev1.ConfigMap{ |
| 110 | + ObjectMeta: metav1.ObjectMeta{ |
| 111 | + Namespace: nsn.Namespace, |
| 112 | + Name: nsn.Name, |
| 113 | + }, |
| 114 | + Data: map[string]string{ |
| 115 | + "application.yaml": string(cust), |
| 116 | + }, |
| 117 | + } |
| 118 | + if err := a.kubeclient.Create(ctx, &cm); err != nil { |
| 119 | + return fmt.Errorf("unable to create kotsadm-application configmap: %w", err) |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | + logrus.Infof("Updating admin console customization config map") |
| 124 | + cm.Data["application.yaml"] = string(cust) |
| 125 | + if err := a.kubeclient.Update(ctx, &cm); err != nil { |
| 126 | + return fmt.Errorf("unable to update kotsadm-application configmap: %w", err) |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
0 commit comments