Skip to content

Commit dfd2790

Browse files
authored
Adds tolerations from fleet-controller when bootstraping and when running the fleet apply job (rancher#3362)
Adding the toleration needed to the helm chart in Fleet is not enough when running the agent and the fleet apply job. This PR adds the tolerations found in the `fleet-controller` deployment to the agent and to the fleet apply job. Refers to: rancher#3313 Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
1 parent 8684928 commit dfd2790

8 files changed

Lines changed: 252 additions & 33 deletions

File tree

charts/fleet/templates/rbac.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ rules:
3838
- 'events'
3939
verbs:
4040
- '*'
41+
- apiGroups:
42+
- "apps"
43+
resources:
44+
- 'deployments'
45+
verbs:
46+
- 'list'
47+
- 'get'
4148
---
4249
apiVersion: rbac.authorization.k8s.io/v1
4350
kind: ClusterRoleBinding

charts/fleet/templates/rbac_gitjob.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ rules:
9999
- list
100100
- watch
101101
- update
102-
102+
- apiGroups:
103+
- "apps"
104+
resources:
105+
- 'deployments'
106+
verbs:
107+
- 'list'
108+
- 'get'
109+
- 'watch'
103110
---
104111
apiVersion: rbac.authorization.k8s.io/v1
105112
kind: ClusterRoleBinding

integrationtests/gitjob/controller/suite_test.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
gomegatypes "github.com/onsi/gomega/types"
1515
"github.com/reugn/go-quartz/quartz"
1616
"go.uber.org/mock/gomock"
17+
appsv1 "k8s.io/api/apps/v1"
18+
corev1 "k8s.io/api/core/v1"
1719

1820
"github.com/rancher/fleet/internal/cmd/controller/gitops/reconciler"
1921
ctrlreconciler "github.com/rancher/fleet/internal/cmd/controller/reconciler"
@@ -100,20 +102,53 @@ var _ = BeforeSuite(func() {
100102
},
101103
)
102104

105+
// fleet-controller deployment
106+
err = k8sClient.Create(ctx, &appsv1.Deployment{
107+
ObjectMeta: metav1.ObjectMeta{
108+
Name: config.ManagerConfigName,
109+
Namespace: "default",
110+
},
111+
Spec: appsv1.DeploymentSpec{
112+
Selector: &metav1.LabelSelector{
113+
MatchLabels: map[string]string{
114+
"app": "fleet-controller",
115+
},
116+
},
117+
Template: corev1.PodTemplateSpec{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Labels: map[string]string{
120+
"app": "fleet-controller",
121+
},
122+
},
123+
Spec: corev1.PodSpec{
124+
Containers: []corev1.Container{
125+
{
126+
Name: "test",
127+
Image: "test", // value is required. but we don't need a real deployment for the test
128+
129+
},
130+
},
131+
},
132+
},
133+
},
134+
})
135+
Expect(err).ToNot(HaveOccurred())
136+
103137
sched := quartz.NewStdScheduler()
104138
Expect(sched).ToNot(BeNil())
105139

106140
config.Set(&config.Config{})
107141

108142
err = (&reconciler.GitJobReconciler{
109-
Client: mgr.GetClient(),
110-
Scheme: mgr.GetScheme(),
111-
Image: "image",
112-
Scheduler: sched,
113-
GitFetcher: fetcherMock,
114-
Clock: reconciler.RealClock{},
115-
Recorder: mgr.GetEventRecorderFor("gitjob-controller"),
116-
Workers: 50,
143+
Client: mgr.GetClient(),
144+
Scheme: mgr.GetScheme(),
145+
Image: "image",
146+
Scheduler: sched,
147+
GitFetcher: fetcherMock,
148+
Clock: reconciler.RealClock{},
149+
Recorder: mgr.GetEventRecorderFor("gitjob-controller"),
150+
Workers: 50,
151+
SystemNamespace: "default",
117152
}).SetupWithManager(mgr)
118153
Expect(err).ToNot(HaveOccurred())
119154

internal/cmd/controller/agentmanagement/controllers/bootstrap/bootstrap.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import (
1010

1111
secretutil "github.com/rancher/fleet/internal/cmd/controller/agentmanagement/secret"
1212
fleetns "github.com/rancher/fleet/internal/cmd/controller/namespace"
13-
"github.com/rancher/fleet/internal/config"
13+
fleetconfig "github.com/rancher/fleet/internal/config"
1414
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
1515
"github.com/rancher/wrangler/v3/pkg/apply"
16+
appscontrollers "github.com/rancher/wrangler/v3/pkg/generated/controllers/apps/v1"
1617
corecontrollers "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
1718

1819
corev1 "k8s.io/api/core/v1"
1920
apierrors "k8s.io/apimachinery/pkg/api/errors"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
2123
"k8s.io/apimachinery/pkg/runtime"
2224
"k8s.io/client-go/rest"
2325
"k8s.io/client-go/tools/clientcmd"
@@ -39,6 +41,7 @@ type handler struct {
3941
serviceAccountCache corecontrollers.ServiceAccountCache
4042
secretsCache corecontrollers.SecretCache
4143
secretsController corecontrollers.SecretController
44+
deploymentsCache appscontrollers.DeploymentCache
4245
cfg clientcmd.ClientConfig
4346
}
4447

@@ -49,19 +52,21 @@ func Register(ctx context.Context,
4952
serviceAccountCache corecontrollers.ServiceAccountCache,
5053
secretsController corecontrollers.SecretController,
5154
secretsCache corecontrollers.SecretCache,
55+
deploymentCache appscontrollers.DeploymentCache,
5256
) {
5357
h := handler{
5458
systemNamespace: systemNamespace,
5559
serviceAccountCache: serviceAccountCache,
5660
secretsCache: secretsCache,
5761
secretsController: secretsController,
62+
deploymentsCache: deploymentCache,
5863
apply: apply.WithSetID("fleet-bootstrap"),
5964
cfg: cfg,
6065
}
61-
config.OnChange(ctx, h.OnConfig)
66+
fleetconfig.OnChange(ctx, h.OnConfig)
6267
}
6368

64-
func (h *handler) OnConfig(config *config.Config) error {
69+
func (h *handler) OnConfig(config *fleetconfig.Config) error {
6570
logrus.Debugf("Bootstrap config set, building namespace '%s', secret, local cluster, cluster group, ...", config.Bootstrap.Namespace)
6671

6772
var objs []runtime.Object
@@ -74,6 +79,10 @@ func (h *handler) OnConfig(config *config.Config) error {
7479
if err != nil {
7580
return err
7681
}
82+
fleetControllerDeployment, err := h.deploymentsCache.Get(h.systemNamespace, fleetconfig.ManagerConfigName)
83+
if err != nil {
84+
return err
85+
}
7786
objs = append(objs, &corev1.Namespace{
7887
ObjectMeta: metav1.ObjectMeta{
7988
Name: config.Bootstrap.Namespace,
@@ -89,6 +98,8 @@ func (h *handler) OnConfig(config *config.Config) error {
8998
Spec: fleet.ClusterSpec{
9099
KubeConfigSecret: secret.Name,
91100
AgentNamespace: config.Bootstrap.AgentNamespace,
101+
// copy tolerations from fleet-controller
102+
AgentTolerations: fleetControllerDeployment.Spec.Template.Spec.Tolerations,
92103
},
93104
}, &fleet.ClusterGroup{
94105
ObjectMeta: metav1.ObjectMeta{
@@ -164,9 +175,9 @@ func (h *handler) buildSecret(bootstrapNamespace string, cfg clientcmd.ClientCon
164175
},
165176
},
166177
Data: map[string][]byte{
167-
config.KubeConfigSecretValueKey: value,
168-
config.APIServerURLKey: []byte(host),
169-
config.APIServerCAKey: ca,
178+
fleetconfig.KubeConfigSecretValueKey: value,
179+
fleetconfig.APIServerURLKey: []byte(host),
180+
fleetconfig.APIServerCAKey: ca,
170181
},
171182
}, nil
172183
}

internal/cmd/controller/agentmanagement/controllers/controllers.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/rancher/lasso/pkg/client"
2121
"github.com/rancher/lasso/pkg/controller"
2222
"github.com/rancher/wrangler/v3/pkg/apply"
23+
"github.com/rancher/wrangler/v3/pkg/generated/controllers/apps"
24+
appscontrollers "github.com/rancher/wrangler/v3/pkg/generated/controllers/apps/v1"
2325
"github.com/rancher/wrangler/v3/pkg/generated/controllers/core"
2426
corecontrollers "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
2527
"github.com/rancher/wrangler/v3/pkg/generated/controllers/rbac"
@@ -42,6 +44,7 @@ type AppContext struct {
4244

4345
K8s kubernetes.Interface
4446
Core corecontrollers.Interface
47+
Apps appscontrollers.Interface
4548
RBAC rbaccontrollers.Interface
4649
RESTMapper meta.RESTMapper
4750
Apply apply.Apply
@@ -86,7 +89,8 @@ func Register(ctx context.Context, appCtx *AppContext, systemNamespace string, d
8689
appCtx.ClientConfig,
8790
appCtx.Core.ServiceAccount().Cache(),
8891
appCtx.Core.Secret(),
89-
appCtx.Core.Secret().Cache())
92+
appCtx.Core.Secret().Cache(),
93+
appCtx.Apps.Deployment().Cache())
9094
}
9195

9296
cluster.Register(ctx,
@@ -182,6 +186,14 @@ func NewAppContext(cfg clientcmd.ClientConfig) (*AppContext, error) {
182186
}
183187
rbacv := rbac.Rbac().V1()
184188

189+
apps, err := apps.NewFactoryFromConfigWithOptions(client, &apps.FactoryOptions{
190+
SharedControllerFactory: scf,
191+
})
192+
if err != nil {
193+
return nil, err
194+
}
195+
appsv := apps.Apps().V1()
196+
185197
apply, err := apply.NewForConfig(client)
186198
if err != nil {
187199
return nil, err
@@ -197,6 +209,7 @@ func NewAppContext(cfg clientcmd.ClientConfig) (*AppContext, error) {
197209
K8s: k8s,
198210
Interface: fleetv,
199211
Core: corev,
212+
Apps: appsv,
200213
RBAC: rbacv,
201214
Apply: apply,
202215
ClientConfig: cfg,

internal/cmd/controller/gitops/operator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func (g *GitOperator) Run(cmd *cobra.Command, args []string) error {
144144
GitFetcher: &git.Fetch{},
145145
Clock: reconciler.RealClock{},
146146
Recorder: mgr.GetEventRecorderFor(fmt.Sprintf("fleet-gitops%s", shardIDSuffix)),
147+
SystemNamespace: namespace,
147148
}
148149

149150
statusReconciler := &reconciler.StatusReconciler{

internal/cmd/controller/gitops/reconciler/gitjob_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
fleetutil "github.com/rancher/fleet/internal/cmd/controller/errorutil"
1919
"github.com/rancher/fleet/internal/cmd/controller/finalize"
2020
"github.com/rancher/fleet/internal/cmd/controller/imagescan"
21+
"github.com/rancher/fleet/internal/config"
2122
"github.com/rancher/fleet/internal/metrics"
2223
"github.com/rancher/fleet/internal/names"
2324
"github.com/rancher/fleet/internal/ociwrapper"
@@ -30,6 +31,7 @@ import (
3031
"github.com/rancher/wrangler/v3/pkg/genericcondition"
3132
"github.com/rancher/wrangler/v3/pkg/kstatus"
3233

34+
appsv1 "k8s.io/api/apps/v1"
3335
batchv1 "k8s.io/api/batch/v1"
3436
corev1 "k8s.io/api/core/v1"
3537
"k8s.io/apimachinery/pkg/api/equality"
@@ -98,6 +100,7 @@ type GitJobReconciler struct {
98100
GitFetcher GitFetcher
99101
Clock TimeGetter
100102
Recorder record.EventRecorder
103+
SystemNamespace string
101104
}
102105

103106
func (r *GitJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
@@ -562,7 +565,19 @@ func (r *GitJobReconciler) newGitJob(ctx context.Context, obj *v1alpha1.GitRepo)
562565
if err != nil {
563566
return nil, err
564567
}
568+
var fleetControllerDeployment appsv1.Deployment
569+
if err := r.Get(ctx, types.NamespacedName{
570+
Namespace: r.SystemNamespace,
571+
Name: config.ManagerConfigName,
572+
}, &fleetControllerDeployment); err != nil {
573+
return nil, err
574+
}
565575

576+
// add tolerations from the fleet-controller deployment
577+
jobSpec.Template.Spec.Tolerations = append(
578+
jobSpec.Template.Spec.Tolerations,
579+
fleetControllerDeployment.Spec.Template.Spec.Tolerations...,
580+
)
566581
job := &batchv1.Job{
567582
ObjectMeta: metav1.ObjectMeta{
568583
Annotations: map[string]string{

0 commit comments

Comments
 (0)