in operator/scripts/update-kcc-manifest/main.go [61:282]
func main() {
ctx := context.TODO()
flag.StringVar(&version, "version", "latest", "Version of the KCC core to download.")
flag.Parse()
// download the KCC manifest
operatorSrcRoot := paths.GetOperatorSrcRootOrLogFatal()
log.Printf("Operator source root is set to %s.\r\n", operatorSrcRoot)
outputDir := path.Join(operatorSrcRoot, baseDir, "kcc")
if err := os.Mkdir(outputDir, dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir %v: %w", outputDir, err))
}
if version == "local" {
tarballPath := path.Join("..", "release-bundle.tar.gz")
log.Printf("Extracting bundle resource from local %s to %s.\r\n", tarballPath, outputDir)
if err := utils.ExtractTarball(tarballPath, outputDir); err != nil {
log.Fatal(fmt.Errorf("error extracting tarball: %w", err))
}
} else {
gcsPath := fmt.Sprintf(gcsPathTmpl, version)
log.Printf("GCS Path is set to %s.\r\n", gcsPath)
if err := utils.DownloadAndExtractTarballAt(gcsPath, outputDir); err != nil {
log.Fatal(fmt.Errorf("error downloading and extracting the tarball %v: %w", gcsPath, err))
}
}
kustomizeBuild(operatorSrcRoot)
// swap container registry
wiSystemManifest := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-workload-identity", "0-cnrm-system.yaml")
gcpSystemManifest := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-gcp-identity", "0-cnrm-system.yaml")
namespacedSystemManifest := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-namespaced", "0-cnrm-system.yaml")
autopilotWiSystemManifest := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-autopilot-workload-identity", "0-cnrm-system.yaml")
autopilotGcpSystemManifest := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-autopilot-gcp-identity", "0-cnrm-system.yaml")
autopilotNamespacedSystemManifest := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-autopilot-namespaced", "0-cnrm-system.yaml")
pnc := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-namespaced", "per-namespace-components.yaml")
manifests := []string{wiSystemManifest, gcpSystemManifest, namespacedSystemManifest, pnc}
for _, manifest := range manifests {
if err := swapContainerRegistry(manifest); err != nil {
log.Fatal(fmt.Errorf("error swapping container registry: %w", err))
}
}
autopilotPnc := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-autopilot-namespaced", "per-namespace-components.yaml")
manifests = []string{autopilotWiSystemManifest, autopilotGcpSystemManifest, autopilotNamespacedSystemManifest, autopilotPnc}
for _, manifest := range manifests {
if err := swapContainerRegistry(manifest); err != nil {
log.Fatal(fmt.Errorf("error swapping container registry: %w", err))
}
}
// upload the new manifest under channels dir
manifestFile := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-namespaced", "0-cnrm-system.yaml")
version, err := extractVersionFromManifest(manifestFile)
if err != nil {
log.Fatal(fmt.Errorf("error extracting version from manifest %v: %w", manifestFile, err))
}
manifestDir := path.Join(operatorSrcRoot, channelDir, version)
if err := os.Mkdir(manifestDir, dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir %v: %w", manifestDir, err))
}
autopilotManifestDir := path.Join(operatorSrcRoot, autopilotChannelDir, version)
if err := os.Mkdir(autopilotManifestDir, dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir %v: %w", autopilotManifestDir, err))
}
// copy crds.yaml
crds := path.Join(operatorSrcRoot, baseDir, "kcc", "install-bundle-namespaced", "crds.yaml")
destCRDs := path.Join(manifestDir, "crds.yaml")
if err := utils.Copy(crds, destCRDs); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", crds, destCRDs, err))
}
destCRDs = path.Join(autopilotManifestDir, "crds.yaml")
if err := utils.Copy(crds, destCRDs); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", crds, destCRDs, err))
}
// create the cluster dir
if err := os.Mkdir(path.Join(manifestDir, "cluster"), dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir: %w", err))
}
if err := os.Mkdir(path.Join(autopilotManifestDir, "cluster"), dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir: %w", err))
}
// copy install-bundle-workload-identity/0-cnrm-system.yaml
if err := os.Mkdir(path.Join(manifestDir, "cluster", "workload-identity"), dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir: %w", err))
}
destWiSystemManifest := path.Join(manifestDir, "cluster", "workload-identity", "0-cnrm-system.yaml")
if err := utils.Copy(wiSystemManifest, destWiSystemManifest); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", wiSystemManifest, destWiSystemManifest, err))
}
if err := os.Mkdir(path.Join(autopilotManifestDir, "cluster", "workload-identity"), dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir: %w", err))
}
destWiSystemManifest = path.Join(autopilotManifestDir, "cluster", "workload-identity", "0-cnrm-system.yaml")
if err := utils.Copy(autopilotWiSystemManifest, destWiSystemManifest); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", wiSystemManifest, destWiSystemManifest, err))
}
// copy install-bundle-gcp-identity/0-cnrm-system.yaml
if err := os.Mkdir(path.Join(manifestDir, "cluster", "gcp-identity"), dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir: %w", err))
}
destGcpSystemManifest := path.Join(manifestDir, "cluster", "gcp-identity", "0-cnrm-system.yaml")
if err := utils.Copy(gcpSystemManifest, destGcpSystemManifest); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", wiSystemManifest, destWiSystemManifest, err))
}
if err := os.Mkdir(path.Join(autopilotManifestDir, "cluster", "gcp-identity"), dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir: %w", err))
}
destGcpSystemManifest = path.Join(autopilotManifestDir, "cluster", "gcp-identity", "0-cnrm-system.yaml")
if err := utils.Copy(autopilotGcpSystemManifest, destGcpSystemManifest); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", wiSystemManifest, destWiSystemManifest, err))
}
// copy install-bundle-namespaced/0-cnrm-system.yaml and install-bundle-namespaced/per-namespace-components.yaml
namespacedDir := path.Join(manifestDir, "namespaced")
if err := os.Mkdir(namespacedDir, dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir %v: %w", namespacedDir, err))
}
destNamespacedSystemManifest := path.Join(manifestDir, "namespaced", "0-cnrm-system.yaml")
if err := utils.Copy(namespacedSystemManifest, destNamespacedSystemManifest); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", namespacedSystemManifest, destNamespacedSystemManifest, err))
}
destPnc := path.Join(manifestDir, "namespaced", "per-namespace-components.yaml")
if err := utils.Copy(pnc, destPnc); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", pnc, destPnc, err))
}
namespacedDir = path.Join(autopilotManifestDir, "namespaced")
if err := os.Mkdir(namespacedDir, dirMode); err != nil && !os.IsExist(err) {
log.Fatal(fmt.Errorf("error creating dir %v: %w", namespacedDir, err))
}
destNamespacedSystemManifest = path.Join(autopilotManifestDir, "namespaced", "0-cnrm-system.yaml")
if err := utils.Copy(autopilotNamespacedSystemManifest, destNamespacedSystemManifest); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", autopilotNamespacedSystemManifest, destNamespacedSystemManifest, err))
}
destPnc = path.Join(autopilotManifestDir, "namespaced", "per-namespace-components.yaml")
if err := utils.Copy(autopilotPnc, destPnc); err != nil {
log.Fatal(fmt.Errorf("error copying %v to %v: %w", autopilotPnc, destPnc, err))
}
if err := os.RemoveAll(outputDir); err != nil {
log.Fatal(fmt.Errorf("error deleting dir %v: %w", outputDir, err))
}
// update the operator version for default kustomization
kustomizationFilePath := path.Join(operatorSrcRoot, "config", "default", "kustomization.yaml")
b, err := ioutil.ReadFile(kustomizationFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error reading %v: %w", kustomizationFilePath, err))
}
kustomization := string(b)
m := regexp.MustCompile("cnrm.cloud.google.com/operator-version: (\".*\")")
kustomization = m.ReplaceAllString(kustomization, fmt.Sprintf("cnrm.cloud.google.com/operator-version: \"%v\"", version))
if err := ioutil.WriteFile(kustomizationFilePath, []byte(kustomization), fileMode); err != nil {
log.Fatalf("error updating file %v", kustomizationFilePath)
}
log.Printf("successfully updated the version annotation in %v for default kustomization\n", kustomizationFilePath)
// update the operator version for autopilot kustomization
kustomizationFilePath = path.Join(operatorSrcRoot, "config", "autopilot", "kustomization.yaml")
b, err = ioutil.ReadFile(kustomizationFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error reading %v: %w", kustomizationFilePath, err))
}
kustomization = string(b)
m = regexp.MustCompile("cnrm.cloud.google.com/operator-version: (\".*\")")
kustomization = m.ReplaceAllString(kustomization, fmt.Sprintf("cnrm.cloud.google.com/operator-version: \"%v\"", version))
if err := ioutil.WriteFile(kustomizationFilePath, []byte(kustomization), fileMode); err != nil {
log.Fatalf("error updating file %v", kustomizationFilePath)
}
log.Printf("successfully updated the version annotation in %v for autopilot kustomization\n", kustomizationFilePath)
//Update the stable version
r := loaders.NewFSRepository(path.Join(operatorSrcRoot, loaders.FlagChannel))
channel, err := r.LoadChannel(ctx, k8s.StableChannel)
if err != nil {
log.Fatal(fmt.Errorf("error loading %v channel: %w", k8s.StableChannel, err))
}
currentVersion, err := channel.Latest(ctx, "configconnector")
if err != nil {
log.Fatal(fmt.Errorf("error resolving the current version: %w", err))
}
/*
if currentVersion.Version == version {
log.Printf("the current KCC version is the same as the latest version %v\n", version)
return
}*/
stableFilePath := path.Join(operatorSrcRoot, "channels", "stable")
b, err = ioutil.ReadFile(stableFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error reading %v: %w", stableFilePath, err))
}
stable := string(b)
stable = strings.ReplaceAll(stable, fmt.Sprintf("- version: %v", currentVersion.Version), fmt.Sprintf("- version: %v", version))
if err := ioutil.WriteFile(stableFilePath, []byte(stable), fileMode); err != nil {
log.Fatalf("error updating file %v", stableFilePath)
}
stableFilePath = path.Join(operatorSrcRoot, "autopilot-channels", "stable")
b, err = ioutil.ReadFile(stableFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error reading %v: %w", stableFilePath, err))
}
stable = string(b)
stable = strings.ReplaceAll(stable, fmt.Sprintf("- version: %v", currentVersion.Version), fmt.Sprintf("- version: %v", version))
if err := ioutil.WriteFile(stableFilePath, []byte(stable), fileMode); err != nil {
log.Fatalf("error updating file %v", stableFilePath)
}
channelDir := path.Join(operatorSrcRoot, "channels", "packages", "configconnector")
if err := dropStalePackages(channelDir); err != nil {
log.Fatalf("drop stale packages: %s", err)
}
autoPilotChannelDir := path.Join(operatorSrcRoot, "autopilot-channels", "packages", "configconnector")
if err := dropStalePackages(autoPilotChannelDir); err != nil {
log.Fatalf("drop stale packages: %s", err)
}
}