|
| 1 | +package configmap |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "regexp" |
| 8 | + |
| 9 | + "github.com/ghodss/yaml" |
| 10 | + _ "github.com/mattn/go-sqlite3" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + batchv1 "k8s.io/api/batch/v1" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | + |
| 17 | + "github.com/operator-framework/operator-registry/pkg/client" |
| 18 | + "github.com/operator-framework/operator-registry/pkg/lib/bundle" |
| 19 | +) |
| 20 | + |
| 21 | +// configmap keys can contain underscores, but configmap names can not |
| 22 | +var unallowedKeyChars = regexp.MustCompile("[^-A-Za-z0-9_.]") |
| 23 | + |
| 24 | +const ( |
| 25 | + EnvContainerImage = "CONTAINER_IMAGE" |
| 26 | + ConfigMapImageAnnotationKey = "olm.sourceImage" |
| 27 | +) |
| 28 | + |
| 29 | +type AnnotationsFile struct { |
| 30 | + Annotations struct { |
| 31 | + Resources string `json:"operators.operatorframework.io.bundle.manifests.v1"` |
| 32 | + MediaType string `json:"operators.operatorframework.io.bundle.mediatype.v1"` |
| 33 | + Metadata string `json:"operators.operatorframework.io.bundle.metadata.v1"` |
| 34 | + Package string `json:"operators.operatorframework.io.bundle.package.v1"` |
| 35 | + Channels string `json:"operators.operatorframework.io.bundle.channels.v1"` |
| 36 | + ChannelDefault string `json:"operators.operatorframework.io.bundle.channel.default.v1"` |
| 37 | + } `json:"annotations"` |
| 38 | +} |
| 39 | + |
| 40 | +type ConfigMapWriter struct { |
| 41 | + manifestsDir string |
| 42 | + configMapName string |
| 43 | + namespace string |
| 44 | + clientset *kubernetes.Clientset |
| 45 | +} |
| 46 | + |
| 47 | +func NewConfigMapLoaderForDirectory(configMapName, namespace, manifestsDir, kubeconfig string) *ConfigMapWriter { |
| 48 | + clientset, err := client.NewKubeClient(kubeconfig, logrus.StandardLogger()) |
| 49 | + if err != nil { |
| 50 | + logrus.Fatalf("cluster config failed: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + return &ConfigMapWriter{ |
| 54 | + manifestsDir: manifestsDir, |
| 55 | + configMapName: configMapName, |
| 56 | + namespace: namespace, |
| 57 | + clientset: clientset, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TranslateInvalidChars(input string) string { |
| 62 | + validConfigMapKey := unallowedKeyChars.ReplaceAllString(input, "~") |
| 63 | + return validConfigMapKey |
| 64 | +} |
| 65 | + |
| 66 | +func (c *ConfigMapWriter) Populate(maxDataSizeLimit uint64) error { |
| 67 | + subDirs := []string{"manifests/", "metadata/"} |
| 68 | + |
| 69 | + configMapPopulate, err := c.clientset.CoreV1().ConfigMaps(c.namespace).Get(c.configMapName, metav1.GetOptions{}) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + configMapPopulate.Data = map[string]string{} |
| 74 | + |
| 75 | + var totalSize uint64 |
| 76 | + for _, dir := range subDirs { |
| 77 | + completePath := c.manifestsDir + dir |
| 78 | + files, err := ioutil.ReadDir(completePath) |
| 79 | + if err != nil { |
| 80 | + logrus.Errorf("read dir failed: %v", err) |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + for _, file := range files { |
| 85 | + log := logrus.WithField("file", completePath+file.Name()) |
| 86 | + log.Info("Reading file") |
| 87 | + content, err := ioutil.ReadFile(completePath + file.Name()) |
| 88 | + if err != nil { |
| 89 | + log.Errorf("read failed: %v", err) |
| 90 | + return err |
| 91 | + } |
| 92 | + totalSize += uint64(len(content)) |
| 93 | + if totalSize > maxDataSizeLimit { |
| 94 | + log.Errorf("File with size %v exceeded %v limit, aboring", len(content), maxDataSizeLimit) |
| 95 | + return fmt.Errorf("file %v bigger than total allowed limit", file.Name()) |
| 96 | + } |
| 97 | + |
| 98 | + validConfigMapKey := TranslateInvalidChars(file.Name()) |
| 99 | + if validConfigMapKey != file.Name() { |
| 100 | + logrus.WithFields(logrus.Fields{ |
| 101 | + "file.Name": file.Name(), |
| 102 | + "validConfigMapKey": validConfigMapKey, |
| 103 | + }).Info("translated filename for configmap comptability") |
| 104 | + } |
| 105 | + if file.Name() == bundle.AnnotationsFile { |
| 106 | + var annotationsFile AnnotationsFile |
| 107 | + err := yaml.Unmarshal(content, &annotationsFile) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + configMapPopulate.SetAnnotations(map[string]string{ |
| 112 | + bundle.ManifestsLabel: annotationsFile.Annotations.Resources, |
| 113 | + bundle.MediatypeLabel: annotationsFile.Annotations.MediaType, |
| 114 | + bundle.MetadataLabel: annotationsFile.Annotations.Metadata, |
| 115 | + bundle.PackageLabel: annotationsFile.Annotations.Package, |
| 116 | + bundle.ChannelsLabel: annotationsFile.Annotations.Channels, |
| 117 | + bundle.ChannelDefaultLabel: annotationsFile.Annotations.ChannelDefault, |
| 118 | + }) |
| 119 | + } else { |
| 120 | + configMapPopulate.Data[validConfigMapKey] = string(content) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if sourceImage := os.Getenv(EnvContainerImage); sourceImage != "" { |
| 126 | + annotations := configMapPopulate.GetAnnotations() |
| 127 | + annotations[ConfigMapImageAnnotationKey] = sourceImage |
| 128 | + } |
| 129 | + |
| 130 | + _, err = c.clientset.CoreV1().ConfigMaps(c.namespace).Update(configMapPopulate) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// LaunchBundleImage will launch a bundle image and also create a configmap for |
| 138 | +// storing the data that will be updated to contain the bundle image data. It is |
| 139 | +// the responsibility of the caller to delete the job, the pod, and the configmap |
| 140 | +// when done. This function is intended to be called from OLM, but is put here |
| 141 | +// for locality. |
| 142 | +func LaunchBundleImage(kubeclient kubernetes.Interface, bundleImage, initImage, namespace string) (*corev1.ConfigMap, *batchv1.Job, error) { |
| 143 | + // create configmap for bundle image data to write to (will be returned) |
| 144 | + newConfigMap, err := kubeclient.CoreV1().ConfigMaps(namespace).Create(&corev1.ConfigMap{ |
| 145 | + ObjectMeta: metav1.ObjectMeta{ |
| 146 | + GenerateName: "bundle-image-", |
| 147 | + }, |
| 148 | + }) |
| 149 | + if err != nil { |
| 150 | + return nil, nil, err |
| 151 | + } |
| 152 | + |
| 153 | + launchJob := batchv1.Job{ |
| 154 | + ObjectMeta: metav1.ObjectMeta{ |
| 155 | + GenerateName: "deploy-bundle-image-", |
| 156 | + }, |
| 157 | + Spec: batchv1.JobSpec{ |
| 158 | + //ttlSecondsAfterFinished: 0 // can use in the future to not have to clean up job |
| 159 | + Template: corev1.PodTemplateSpec{ |
| 160 | + ObjectMeta: metav1.ObjectMeta{ |
| 161 | + Name: "bundle-image", |
| 162 | + }, |
| 163 | + Spec: corev1.PodSpec{ |
| 164 | + RestartPolicy: corev1.RestartPolicyOnFailure, |
| 165 | + Containers: []corev1.Container{ |
| 166 | + { |
| 167 | + Name: "bundle-image", |
| 168 | + Image: bundleImage, |
| 169 | + ImagePullPolicy: "Never", |
| 170 | + Command: []string{"/injected/opm", "alpha", "bundle", "extract", "-n", namespace, "-c", newConfigMap.GetName()}, |
| 171 | + Env: []corev1.EnvVar{ |
| 172 | + { |
| 173 | + Name: EnvContainerImage, |
| 174 | + Value: bundleImage, |
| 175 | + }, |
| 176 | + }, |
| 177 | + VolumeMounts: []corev1.VolumeMount{ |
| 178 | + { |
| 179 | + Name: "copydir", |
| 180 | + MountPath: "/injected", |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + InitContainers: []corev1.Container{ |
| 186 | + { |
| 187 | + Name: "copy-binary", |
| 188 | + Image: initImage, |
| 189 | + ImagePullPolicy: "Never", |
| 190 | + Command: []string{"/bin/cp", "opm", "/copy-dest"}, |
| 191 | + VolumeMounts: []corev1.VolumeMount{ |
| 192 | + { |
| 193 | + Name: "copydir", |
| 194 | + MountPath: "/copy-dest", |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + Volumes: []corev1.Volume{ |
| 200 | + { |
| 201 | + Name: "copydir", |
| 202 | + VolumeSource: corev1.VolumeSource{ |
| 203 | + EmptyDir: &corev1.EmptyDirVolumeSource{}, |
| 204 | + }, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
| 210 | + } |
| 211 | + launchedJob, err := kubeclient.BatchV1().Jobs(namespace).Create(&launchJob) |
| 212 | + if err != nil { |
| 213 | + err := kubeclient.CoreV1().ConfigMaps(namespace).Delete(newConfigMap.GetName(), &metav1.DeleteOptions{}) |
| 214 | + if err != nil { |
| 215 | + // already in an error, so just report it |
| 216 | + logrus.Errorf("failed to remove configmap: %v", err) |
| 217 | + } |
| 218 | + return nil, nil, err |
| 219 | + } |
| 220 | + |
| 221 | + return newConfigMap, launchedJob, nil |
| 222 | +} |
0 commit comments