github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/apis/imagemap/imagemap.go (about) 1 package imagemap 2 3 import ( 4 "context" 5 "fmt" 6 7 "k8s.io/apimachinery/pkg/types" 8 9 apierrors "k8s.io/apimachinery/pkg/api/errors" 10 ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" 11 12 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 13 "github.com/tilt-dev/tilt/pkg/model" 14 ) 15 16 // Inject completed ImageMaps into the environment of a local process that 17 // wants to deploy to a cluster. 18 // 19 // Current env vars: 20 // TILT_IMAGE_i - The reference to the image #i from the point of view of the cluster container runtime. 21 // TILT_IMAGE_MAP_i - The name of the image map #i with the current status of the image. 22 // 23 // where an env may depend on arbitrarily many image maps. 24 func InjectIntoDeployEnv(cmd *model.Cmd, imageMapNames []string, imageMaps map[types.NamespacedName]*v1alpha1.ImageMap) error { 25 for i, imageMapName := range imageMapNames { 26 imageMap, ok := imageMaps[types.NamespacedName{Name: imageMapName}] 27 if !ok { 28 return fmt.Errorf("internal error: missing imagemap %s", imageMapName) 29 } 30 31 cmd.Env = append(cmd.Env, fmt.Sprintf("TILT_IMAGE_MAP_%d=%s", i, imageMapName)) 32 cmd.Env = append(cmd.Env, fmt.Sprintf("TILT_IMAGE_%d=%s", i, imageMap.Status.ImageFromCluster)) 33 } 34 return nil 35 } 36 37 // Inject completed ImageMaps into the environment of a local process that 38 // wants to build images locally. 39 // 40 // Current env vars: 41 // TILT_IMAGE_i - The reference to the image #i from the point of view of the local host. 42 // TILT_IMAGE_MAP_i - The name of the image map #i with the current status of the image. 43 // 44 // where an env may depend on arbitrarily many image maps. 45 func InjectIntoLocalEnv(cmd *v1alpha1.Cmd, imageMapNames []string, imageMaps map[types.NamespacedName]*v1alpha1.ImageMap) (*v1alpha1.Cmd, error) { 46 cmd = cmd.DeepCopy() 47 for i, imageMapName := range imageMapNames { 48 imageMap, ok := imageMaps[types.NamespacedName{Name: imageMapName}] 49 if !ok { 50 return nil, fmt.Errorf("internal error: missing imagemap %s", imageMapName) 51 } 52 53 cmd.Spec.Env = append(cmd.Spec.Env, fmt.Sprintf("TILT_IMAGE_MAP_%d=%s", i, imageMapName)) 54 cmd.Spec.Env = append(cmd.Spec.Env, fmt.Sprintf("TILT_IMAGE_%d=%s", i, imageMap.Status.ImageFromLocal)) 55 } 56 return cmd, nil 57 } 58 59 // Populate a map with all the given imagemaps, skipping any that don't exist 60 func NamesToObjects(ctx context.Context, client ctrlclient.Client, names []string) (map[types.NamespacedName]*v1alpha1.ImageMap, error) { 61 imageMaps := make(map[types.NamespacedName]*v1alpha1.ImageMap) 62 for _, name := range names { 63 var im v1alpha1.ImageMap 64 nn := types.NamespacedName{Name: name} 65 err := client.Get(ctx, nn, &im) 66 if err != nil { 67 if apierrors.IsNotFound(err) { 68 // If the map isn't found, keep going 69 continue 70 } 71 return nil, err 72 } 73 74 imageMaps[nn] = &im 75 } 76 return imageMaps, nil 77 }