|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package genutil |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 22 | + "github.com/operator-framework/operator-sdk/internal/generate/collector" |
| 23 | + log "github.com/sirupsen/logrus" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/util/sets" |
| 26 | +) |
| 27 | + |
| 28 | +// FindRelatedImages looks in the controller manager's environment for images used by the operator. |
| 29 | +func FindRelatedImages(manifestCol *collector.Manifests) ([]operatorsv1alpha1.RelatedImage, error) { |
| 30 | + col := relatedImageCollector{ |
| 31 | + relatedImages: []*relatedImage{}, |
| 32 | + relatedImagesByName: make(map[string][]*relatedImage), |
| 33 | + relatedImagesByImageRef: make(map[string][]*relatedImage), |
| 34 | + seenRelatedImages: sets.NewString(), |
| 35 | + } |
| 36 | + |
| 37 | + for _, deployment := range manifestCol.Deployments { |
| 38 | + containers := append(deployment.Spec.Template.Spec.Containers, deployment.Spec.Template.Spec.InitContainers...) |
| 39 | + for _, container := range containers { |
| 40 | + // containerRef can just be the deployment if there's only one container |
| 41 | + // otherwise we need {{ deployment.Name }}-{{ container.Name }} |
| 42 | + containerRef := deployment.Name |
| 43 | + if len(containers) > 1 { |
| 44 | + containerRef += "-" + container.Name |
| 45 | + } |
| 46 | + |
| 47 | + if err := col.collectFromEnvironment(containerRef, container.Env); err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return col.collectedRelatedImages(), nil |
| 54 | +} |
| 55 | + |
| 56 | +const relatedImagePrefix = "RELATED_IMAGE_" |
| 57 | + |
| 58 | +type relatedImage struct { |
| 59 | + name string |
| 60 | + imageRef string |
| 61 | + containerRef string // If 1 container then {{deployment}} else {{deployment}}-{{container}} |
| 62 | +} |
| 63 | + |
| 64 | +type relatedImageCollector struct { |
| 65 | + relatedImages []*relatedImage |
| 66 | + relatedImagesByName map[string][]*relatedImage |
| 67 | + relatedImagesByImageRef map[string][]*relatedImage |
| 68 | + seenRelatedImages sets.String |
| 69 | +} |
| 70 | + |
| 71 | +func (c *relatedImageCollector) collectFromEnvironment(containerRef string, env []corev1.EnvVar) error { |
| 72 | + for _, envVar := range env { |
| 73 | + if strings.HasPrefix(envVar.Name, relatedImagePrefix) { |
| 74 | + if envVar.ValueFrom != nil { |
| 75 | + return fmt.Errorf("related images with valueFrom field unsupported, found in %s`", envVar.Name) |
| 76 | + } |
| 77 | + |
| 78 | + name := c.formatName(envVar.Name) |
| 79 | + c.collect(name, envVar.Value, containerRef) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *relatedImageCollector) collect(name, imageRef, containerRef string) { |
| 87 | + // Don't add exact duplicates (same name and image) |
| 88 | + key := name + "-" + imageRef |
| 89 | + if c.seenRelatedImages.Has(key) { |
| 90 | + return |
| 91 | + } |
| 92 | + c.seenRelatedImages.Insert(key) |
| 93 | + |
| 94 | + relatedImg := relatedImage{ |
| 95 | + name: name, |
| 96 | + imageRef: imageRef, |
| 97 | + containerRef: containerRef, |
| 98 | + } |
| 99 | + |
| 100 | + c.relatedImages = append(c.relatedImages, &relatedImg) |
| 101 | + if relatedImages, ok := c.relatedImagesByName[name]; ok { |
| 102 | + c.relatedImagesByName[name] = append(relatedImages, &relatedImg) |
| 103 | + } else { |
| 104 | + c.relatedImagesByName[name] = []*relatedImage{&relatedImg} |
| 105 | + } |
| 106 | + |
| 107 | + if relatedImages, ok := c.relatedImagesByImageRef[imageRef]; ok { |
| 108 | + c.relatedImagesByImageRef[imageRef] = append(relatedImages, &relatedImg) |
| 109 | + } else { |
| 110 | + c.relatedImagesByImageRef[imageRef] = []*relatedImage{&relatedImg} |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (c *relatedImageCollector) collectedRelatedImages() []operatorsv1alpha1.RelatedImage { |
| 115 | + final := make([]operatorsv1alpha1.RelatedImage, 0, len(c.relatedImages)) |
| 116 | + |
| 117 | + for _, relatedImage := range c.relatedImages { |
| 118 | + name := relatedImage.name |
| 119 | + |
| 120 | + // Prefix the name with the containerRef on name collisions. |
| 121 | + if len(c.relatedImagesByName[relatedImage.name]) > 1 { |
| 122 | + name = relatedImage.containerRef + "-" + name |
| 123 | + } |
| 124 | + |
| 125 | + // Only add the related image to the final list if it's the first occurrence of an image. |
| 126 | + // Blank out the name since the image is used multiple times and warn the user. |
| 127 | + // Multiple containers using she same related image should use the same exact name. |
| 128 | + if relatedImages := c.relatedImagesByImageRef[relatedImage.imageRef]; len(relatedImages) > 1 { |
| 129 | + if relatedImages[0].name != relatedImage.name { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + name = "" |
| 134 | + log.Warnf( |
| 135 | + "warning: multiple related images with the same image ref, %q, and different names found."+ |
| 136 | + "The image will only be listed once with an empty name."+ |
| 137 | + "It is recmmended to either remove the duplicate or use the exact same name.", |
| 138 | + relatedImage.name, |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + final = append(final, operatorsv1alpha1.RelatedImage{Name: name, Image: relatedImage.imageRef}) |
| 143 | + } |
| 144 | + |
| 145 | + return final |
| 146 | +} |
| 147 | + |
| 148 | +// formatName transforms RELATED_IMAGE_This_IS_a_cool_image to this-is-a-cool-image |
| 149 | +func (c *relatedImageCollector) formatName(name string) string { |
| 150 | + return strings.ToLower(strings.Replace(strings.TrimPrefix(name, relatedImagePrefix), "_", "-", -1)) |
| 151 | +} |
0 commit comments