istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/docker-builder/common.go (about)

     1  // Copyright Istio 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 main
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  func extractTags(a Args, target, variant string, hasDoubleDefault bool) []string {
    23  	tags := []string{}
    24  	for _, h := range a.Hubs {
    25  		for _, tg := range a.Tags {
    26  			if variant == DefaultVariant {
    27  				// For default, we have no suffix
    28  				tags = append(tags, fmt.Sprintf("%s/%s:%s%s", h, target, tg, a.suffix))
    29  			} else {
    30  				// Otherwise, we have a suffix with the variant
    31  				tags = append(tags, fmt.Sprintf("%s/%s:%s-%s%s", h, target, tg, variant, a.suffix))
    32  				// If we need a default as well, add it as a second tag for the same image to avoid building twice
    33  				if variant == PrimaryVariant && hasDoubleDefault {
    34  					tags = append(tags, fmt.Sprintf("%s/%s:%s%s", h, target, tg, a.suffix))
    35  				}
    36  			}
    37  		}
    38  	}
    39  	return tags
    40  }
    41  
    42  func createArgs(args Args, target string, variant string, architecture string) map[string]string {
    43  	baseDist := variant
    44  	if baseDist == DefaultVariant {
    45  		baseDist = PrimaryVariant
    46  	}
    47  	m := map[string]string{
    48  		// Base version defines the tag of the base image to use. Typically, set in the Makefile and not overridden.
    49  		"BASE_VERSION": args.BaseVersion,
    50  		// Registry where the base image is stored
    51  		"ISTIO_BASE_REGISTRY": args.BaseImageRegistry,
    52  		// Base distribution picks which variant to build
    53  		"BASE_DISTRIBUTION": baseDist,
    54  		// Additional metadata injected into some images
    55  		"proxy_version":    args.ProxyVersion,
    56  		"ztunnel_version":  args.ZtunnelVersion,
    57  		"istio_version":    args.IstioVersion,
    58  		"VM_IMAGE_NAME":    vmImageName(target),
    59  		"VM_IMAGE_VERSION": vmImageVersion(target),
    60  	}
    61  	// Only needed for crane - buildx does it automagically
    62  	if architecture != "" {
    63  		os, arch, _ := strings.Cut(architecture, "/")
    64  		m["TARGETARCH"] = arch
    65  		m["TARGETOS"] = os
    66  	}
    67  	return m
    68  }
    69  
    70  func vmImageName(target string) string {
    71  	if !strings.HasPrefix(target, "app_sidecar") {
    72  		// Not a VM
    73  		return ""
    74  	}
    75  	if strings.HasPrefix(target, "app_sidecar_base") {
    76  		return strings.Split(target, "_")[3]
    77  	}
    78  
    79  	return strings.Split(target, "_")[2]
    80  }
    81  
    82  func vmImageVersion(target string) string {
    83  	if !strings.HasPrefix(target, "app_sidecar") {
    84  		// Not a VM
    85  		return ""
    86  	}
    87  	if strings.HasPrefix(target, "app_sidecar_base") {
    88  		return strings.Split(target, "_")[4]
    89  	}
    90  
    91  	return strings.Split(target, "_")[3]
    92  }