-
Notifications
You must be signed in to change notification settings - Fork 1
/
status_sync_manager.go
223 lines (190 loc) · 6.78 KB
/
status_sync_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package status_sync
import (
"context"
"embed"
"fmt"
"os"
"github.com/go-logr/logr"
"github.com/openshift/library-go/pkg/assets"
"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilrand "k8s.io/apimachinery/pkg/util/rand"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/component-base/version"
"sigs.k8s.io/controller-runtime/pkg/client"
argov1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
"open-cluster-management.io/addon-framework/pkg/addonmanager"
frameworkagent "open-cluster-management.io/addon-framework/pkg/agent"
"open-cluster-management.io/addon-framework/pkg/utils"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
)
var (
genericScheme = runtime.NewScheme()
genericCodecs = serializer.NewCodecFactory(genericScheme)
genericCodec = genericCodecs.UniversalDeserializer()
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(genericScheme))
utilruntime.Must(argov1alpha1.AddToScheme(genericScheme))
}
const (
templatePath = "manifests/templates"
)
//go:embed manifests
//go:embed manifests/templates
var fs embed.FS
var agentPermissionFiles = []string{
// role with RBAC rules to access resources on hub
"manifests/permission/role.yaml",
// rolebinding to bind the above role to a certain user group
"manifests/permission/rolebinding.yaml",
}
type override struct {
client.Client
log logr.Logger
operatorNamespace string
withOverride bool
}
func NewManagerCommand(componentName string, log logr.Logger) *cobra.Command {
var withOverride bool
runController := func(ctx context.Context, controllerContext *controllercmd.ControllerContext) error {
mgr, err := addonmanager.New(controllerContext.KubeConfig)
if err != nil {
return err
}
registrationOption := newRegistrationOption(
controllerContext.KubeConfig,
controllerContext.EventRecorder,
componentName,
utilrand.String(5),
)
hubClient, err := client.New(controllerContext.KubeConfig, client.Options{Scheme: genericScheme})
if err != nil {
log.Error(err, "failed to create hub client to fetch downstream image override configmap")
return err
}
o := &override{
Client: hubClient,
log: log.WithName("override-values"),
operatorNamespace: controllerContext.OperatorNamespace,
withOverride: withOverride,
}
agentAddon, err := addonfactory.NewAgentAddonFactory(componentName, fs, templatePath).
WithGetValuesFuncs(o.getValueForAgentTemplate, addonfactory.GetValuesFromAddonAnnotation).
WithAgentRegistrationOption(registrationOption).
WithInstallStrategy(frameworkagent.InstallAllStrategy("open-cluster-management-agent-addon")).
BuildTemplateAgentAddon()
if err != nil {
log.Error(err, "failed to build agent")
return err
}
err = mgr.AddAgent(agentAddon)
if err != nil {
log.Error(err, "failed to add agent")
os.Exit(1)
}
err = mgr.Start(ctx)
if err != nil {
log.Error(err, "failed to start addon framework manager")
os.Exit(1)
}
<-ctx.Done()
return nil
}
cmdConfig := controllercmd.
NewControllerCommandConfig(componentName, version.Get(), runController)
cmd := cmdConfig.NewCommand()
cmd.Use = "manager"
cmd.Short = fmt.Sprintf("Start the %s's manager", componentName)
// add disable leader election flag
flags := cmd.Flags()
flags.BoolVar(&cmdConfig.DisableLeaderElection, "disable-leader-election", true, "Disable leader election for the agent.")
flags.BoolVar(&withOverride, "with-image-override", false, "Use image from override configmap")
return cmd
}
func newRegistrationOption(kubeConfig *rest.Config, recorder events.Recorder, componentName, agentName string) *frameworkagent.RegistrationOption {
return &frameworkagent.RegistrationOption{
CSRConfigurations: frameworkagent.KubeClientSignerConfigurations(componentName, agentName),
CSRApproveCheck: utils.DefaultCSRApprover(agentName),
PermissionConfig: func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) error {
kubeclient, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return err
}
for _, file := range agentPermissionFiles {
if err := applyAgentPermissionManifestFromFile(file, cluster.Name, addon.Name, kubeclient, recorder); err != nil {
return err
}
}
return nil
},
}
}
func applyAgentPermissionManifestFromFile(file, clusterName, componentName string, kubeclient *kubernetes.Clientset, recorder events.Recorder) error {
groups := frameworkagent.DefaultGroups(clusterName, componentName)
config := struct {
ClusterName string
Group string
RoleAndRolebindingName string
}{
ClusterName: clusterName,
Group: groups[0],
RoleAndRolebindingName: fmt.Sprintf("open-cluster-management:%s:agent", componentName),
}
results := resourceapply.ApplyDirectly(
context.Background(),
resourceapply.NewKubeClientHolder(kubeclient),
recorder,
resourceapply.NewResourceCache(),
func(name string) ([]byte, error) {
template, err := fs.ReadFile(file)
if err != nil {
return nil, err
}
data := assets.MustCreateAssetFromTemplate(name, template, config).Data
return data, nil
},
file,
)
for _, result := range results {
if result.Error != nil {
return result.Error
}
}
return nil
}
// getValues prepare values for templates at manifests/templates
func (o *override) getValueForAgentTemplate(cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) (addonfactory.Values, error) {
addonImage := os.Getenv("ADDON_IMAGE")
if len(addonImage) == 0 {
addonImage = "quay.io/open-cluster-management/argo-workflow-multicluster:latest"
}
manifestConfig := struct {
KubeConfigSecret string
ClusterName string
AddonName string
AddonInstallNamespace string
Image string
SpokeRolebindingName string
AgentServiceAccountName string
}{
KubeConfigSecret: fmt.Sprintf("%s-hub-kubeconfig", addon.Name),
AddonInstallNamespace: "open-cluster-management-agent-addon",
ClusterName: cluster.Name,
AddonName: fmt.Sprintf("%s-agent", addon.Name),
Image: addonImage,
SpokeRolebindingName: addon.Name,
AgentServiceAccountName: fmt.Sprintf("%s-agent-sa", addon.Name),
}
return addonfactory.StructToValues(manifestConfig), nil
}