...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/scripts/copy-dependency-manifests/main.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/scripts/copy-dependency-manifests

     1  // Copyright 2022 Google LLC
     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  	"context"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"log"
    22  	"path"
    23  
    24  	corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s"
    26  	cnrmmanifest "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/manifest"
    27  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/util/paths"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/scripts/utils"
    29  
    30  	"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
    31  )
    32  
    33  const (
    34  	fileMode = 0644
    35  	rbacDir  = "config/rbac"
    36  )
    37  
    38  func main() {
    39  	if err := run(); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  }
    43  
    44  func run() error {
    45  	manifests, err := loadManifests()
    46  	if err != nil {
    47  		return err
    48  	}
    49  	if err := copyDependencies(manifests); err != nil {
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  func copyDependencies(objects []*manifest.Object) error {
    56  	if err := copyViewerRole(objects); err != nil {
    57  		return err
    58  	}
    59  	return nil
    60  }
    61  
    62  func copyViewerRole(objects []*manifest.Object) error {
    63  	viewerRoleKind := "ClusterRole"
    64  	viewerRoleName := "cnrm-viewer"
    65  	viewerRole, ok := findObject(objects, "ClusterRole", viewerRoleName)
    66  	if !ok {
    67  		return fmt.Errorf("unable to find %v '%v' in manifests", viewerRoleKind, viewerRoleName)
    68  	}
    69  	outputPath := path.Join(paths.GetOperatorSrcRootOrLogFatal(), rbacDir, "cnrm_viewer_role.yaml")
    70  	if err := writeManifestObjectToFile(viewerRole, outputPath); err != nil {
    71  		return err
    72  	}
    73  	return nil
    74  }
    75  
    76  func findObject(objects []*manifest.Object, kind, name string) (*manifest.Object, bool) {
    77  	for _, o := range objects {
    78  		if o.Kind == kind && o.GetName() == name {
    79  			return o, true
    80  		}
    81  	}
    82  	return nil, false
    83  }
    84  
    85  func writeManifestObjectToFile(object *manifest.Object, outputPath string) error {
    86  	bytes, err := utils.UnstructToYaml(object.UnstructuredObject())
    87  	if err != nil {
    88  		return fmt.Errorf("error serializing %v '%v' to yaml", object.Kind, object.GetName())
    89  	}
    90  	if err := ioutil.WriteFile(outputPath, bytes, fileMode); err != nil {
    91  		return fmt.Errorf("error writing unstructured %v '%v' to file", object.Kind, object.GetName())
    92  	}
    93  	return nil
    94  }
    95  
    96  func loadManifests() ([]*manifest.Object, error) {
    97  	ctx := context.Background()
    98  	cc := &corev1beta1.ConfigConnector{
    99  		Spec: corev1beta1.ConfigConnectorSpec{
   100  			Mode: "namespaced",
   101  		},
   102  	}
   103  	operatorSrcRoot := paths.GetOperatorSrcRootOrLogFatal()
   104  	r := cnrmmanifest.NewLocalRepository(path.Join(operatorSrcRoot, "channels"))
   105  	channel, err := r.LoadChannel(ctx, k8s.StableChannel)
   106  	if err != nil {
   107  		return nil, fmt.Errorf("error loading %v channel: %v", k8s.StableChannel, err)
   108  	}
   109  	version, err := channel.Latest(ctx, cc.ComponentName())
   110  	if err != nil {
   111  		return nil, fmt.Errorf("error resolving the version to deploy: %v", err)
   112  	}
   113  	if version == nil {
   114  		return nil, fmt.Errorf("could not find the latest version in channel %v", k8s.StableChannel)
   115  	}
   116  	manifestStrs, err := r.LoadManifest(ctx, cc.ComponentName(), version.Version, cc)
   117  	if err != nil {
   118  		return nil, fmt.Errorf("error loading manifest for package %v of version %v: %v", version.Package, version.Version, err)
   119  	}
   120  	objects := make([]*manifest.Object, 0)
   121  	for _, str := range manifestStrs {
   122  		m, err := manifest.ParseObjects(ctx, str)
   123  		if err != nil {
   124  			return nil, fmt.Errorf("parsing manifest: %v", err)
   125  		}
   126  		objects = append(objects, m.Items...)
   127  	}
   128  	return objects, nil
   129  }
   130  

View as plain text