1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package manifest
16
17 import (
18 "context"
19 "fmt"
20 "io/ioutil"
21 "path/filepath"
22 "strings"
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
27 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/loaders"
28 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
29 "sigs.k8s.io/yaml"
30 )
31
32 type Repository interface {
33 LoadChannel(ctx context.Context, name string) (*loaders.Channel, error)
34 LoadManifest(ctx context.Context, component string, version string, o declarative.DeclarativeObject) (map[string]string, error)
35 LoadNamespacedComponents(ctx context.Context, componentName string, version string) (map[string]string, error)
36 }
37
38 type LocalRepository struct {
39 basedir string
40 }
41
42
43 var _ Repository = &LocalRepository{}
44
45 func NewLocalRepository(basedir string) *LocalRepository {
46 return &LocalRepository{
47 basedir: basedir,
48 }
49 }
50
51 func (r *LocalRepository) LoadChannel(ctx context.Context, name string) (*loaders.Channel, error) {
52 rlog.Info("loading channel", "base", r.basedir, "name", name)
53
54 p := filepath.Join(r.basedir, name)
55 b, err := ioutil.ReadFile(p)
56 if err != nil {
57 rlog.Error(err, "error reading channel", "path", p)
58 return nil, fmt.Errorf("error reading channel %s: %v", p, err)
59 }
60
61 channel := &loaders.Channel{}
62 if err := yaml.Unmarshal(b, channel); err != nil {
63 return nil, fmt.Errorf("error parsing channel %s: %v", p, err)
64 }
65
66 return channel, nil
67 }
68
69 func (r *LocalRepository) LoadManifest(ctx context.Context, componentName string, version string, o declarative.DeclarativeObject) (map[string]string, error) {
70 cc, ok := o.(*corev1beta1.ConfigConnector)
71 if !ok {
72 return nil, fmt.Errorf("expected the resource to be a ConfigConnector, but it was not. Object: %v", o)
73 }
74 mode := cc.GetMode()
75
76 p := filepath.Join(r.basedir, "packages", componentName, version, crdFileName)
77 b, err := ioutil.ReadFile(p)
78 if err != nil {
79 return nil, fmt.Errorf("error reading file %s: %v", p, err)
80 }
81 var sb strings.Builder
82 sb.Write(b)
83 sb.WriteString("---\n")
84 if mode == k8s.ClusterMode {
85 var authIdentity string
86 if cc.Spec.GoogleServiceAccount != "" {
87 authIdentity = "workload-identity"
88 } else {
89 authIdentity = "gcp-identity"
90 }
91 rlog.Info("loading manifest", "component", componentName, "version", version, "mode", mode, "identity", authIdentity)
92 p := filepath.Join(r.basedir, "packages", componentName, version, mode, authIdentity, cnrmSystemFileName)
93 b, err := ioutil.ReadFile(p)
94 if err != nil {
95 return nil, fmt.Errorf("error reading file %s: %v", p, err)
96 }
97 sb.Write(b)
98 path := strings.Join([]string{r.basedir, "packages", componentName, version, mode, authIdentity}, "/")
99 return map[string]string{path: sb.String()}, nil
100 } else {
101 rlog.Info("loading manifest", "component", componentName, "version", version, "mode", mode)
102 p := filepath.Join(r.basedir, "packages", componentName, version, "namespaced", cnrmSystemFileName)
103 b, err := ioutil.ReadFile(p)
104 if err != nil {
105 return nil, fmt.Errorf("error reading file %s: %v", p, err)
106 }
107 sb.Write(b)
108 path := strings.Join([]string{r.basedir, "packages", componentName, version, mode}, "/")
109 return map[string]string{path: sb.String()}, nil
110 }
111 }
112
113 func (r *LocalRepository) LoadNamespacedComponents(ctx context.Context, componentName string, version string) (map[string]string, error) {
114 p := filepath.Join(r.basedir, "packages", componentName, version, "namespaced", perNamespaceComponentsFileName)
115 b, err := ioutil.ReadFile(p)
116 if err != nil {
117 return nil, fmt.Errorf("error reading file %s: %v", p, err)
118 }
119 return map[string]string{p: string(b)}, nil
120 }
121
View as plain text