Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fleet agent deployment configures tolerations from cluster CR #1154

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,27 @@ spec:
agentNamespace:
nullable: true
type: string
agentTolerations:
items:
properties:
effect:
nullable: true
type: string
key:
nullable: true
type: string
operator:
nullable: true
type: string
tolerationSeconds:
nullable: true
type: integer
value:
nullable: true
type: string
type: object
nullable: true
type: array
clientID:
nullable: true
type: string
Expand Down
6 changes: 6 additions & 0 deletions pkg/agent/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ManifestOptions struct {
AgentEnvVars []corev1.EnvVar
AgentImage string
AgentImagePullPolicy string
AgentTolerations []corev1.Toleration
CheckinInterval string
Generation string
PrivateRepoURL string
Expand Down Expand Up @@ -93,6 +94,10 @@ func Manifest(namespace string, agentScope string, opts ManifestOptions) []runti
propagateDebug, _ := strconv.ParseBool(os.Getenv("FLEET_PROPAGATE_DEBUG_SETTINGS_TO_AGENTS"))
debug := logrus.IsLevelEnabled(logrus.DebugLevel) && propagateDebug
dep := agentDeployment(namespace, DefaultName, image, opts.AgentImagePullPolicy, DefaultName, false, debug)

// additional tolerations
dep.Spec.Template.Spec.Tolerations = append(dep.Spec.Template.Spec.Tolerations, opts.AgentTolerations...)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manno it will preserve the tolerations defined by the agentDeployment func and append anything that the user sent. However, it is possible for the user defined tolerations to override the default ones if they have the same key, but I don't see why someone would do that intentionally as the default ones are rancher namespaced.


dep.Spec.Template.Spec.Containers[0].Env = append(dep.Spec.Template.Spec.Containers[0].Env,
corev1.EnvVar{
Name: "AGENT_SCOPE",
Expand Down Expand Up @@ -242,6 +247,7 @@ func agentDeployment(namespace, name, image, imagePullPolicy, serviceAccount str
Value: "linux",
Effect: corev1.TaintEffectNoSchedule,
})

return deployment
}

Expand Down
89 changes: 88 additions & 1 deletion pkg/agent/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package agent

import "testing"
import (
"testing"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestImageResolve(t *testing.T) {
tests := []struct {
Expand All @@ -21,3 +29,82 @@ func TestImageResolve(t *testing.T) {
}
}
}

func getDeploymentFromManifests(namespace string, scope string, opts ManifestOptions) *appsv1.Deployment {
objects := Manifest(namespace, scope, opts)
for _, obj := range objects {
dep, ok := obj.(*appsv1.Deployment)
if ok {
return dep
}
}
return nil
}

func TestManifestAdditionalTolerations(t *testing.T) {
const namespace = "fleet-system"
const scope = "test-scope"
baseOpts := ManifestOptions{
AgentEnvVars: []corev1.EnvVar{},
AgentImage: "rancher/fleet:1.2.3",
AgentImagePullPolicy: "Always",
AgentTolerations: []corev1.Toleration{},
CheckinInterval: "1s",
Generation: "100",
PrivateRepoURL: "private.rancher.com:5000",
SystemDefaultRegistry: "default.rancher.com",
}

// these tolerations should exist regardless of what user sent
baseTolerations := []corev1.Toleration{
{Key: "cattle.io/os", Operator: "Equal", Value: "linux", Effect: "NoSchedule"},
{Key: "node.cloudprovider.kubernetes.io/uninitialized", Operator: "Equal", Value: "true", Effect: "NoSchedule"},
}

less := func(a, b corev1.Toleration) bool { return a.Key < b.Key }
cmpOpt := cmpopts.SortSlices(less)

for _, testCase := range []struct {
name string
getOpts func() ManifestOptions
expectedTolerations []corev1.Toleration
}{
{
name: "BaseOpts",
getOpts: func() ManifestOptions {
return baseOpts
},
expectedTolerations: baseTolerations,
},
{
name: "ExtraToleration",
getOpts: func() ManifestOptions {
withTolerationsOpts := baseOpts
withTolerationsOpts.AgentTolerations = []corev1.Toleration{
{
Key: "fleet-agent",
Operator: "Equals",
Value: "true",
Effect: "NoSchedule",
},
}
return withTolerationsOpts
},
expectedTolerations: append(baseTolerations,
corev1.Toleration{Key: "fleet-agent", Operator: "Equals", Value: "true", Effect: "NoSchedule"},
),
},
} {
t.Run(testCase.name, func(t *testing.T) {

agentDeployment := getDeploymentFromManifests(namespace, scope, testCase.getOpts())
if agentDeployment == nil {
t.Fatal("there were no deployments returned from the manifests")
}

if !cmp.Equal(agentDeployment.Spec.Template.Spec.Tolerations, testCase.expectedTolerations, cmpOpt) {
t.Fatalf("tolerations were not as expected: %v", agentDeployment.Spec.Template.Spec.Tolerations)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ type ClusterSpec struct {

// TemplateValues defines a cluster specific mapping of values to be sent to fleet.yaml values templating
TemplateValues *GenericMap `json:"templateValues,omitempty"`

// AgentTolerations defines an extra set of Tolerations to be added to the Agent deployment
AgentTolerations []v1.Toleration `json:"agentTolerations,omitempty"`
}

type ClusterStatus struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/zz_generated_deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/controllers/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
Labels: cluster.Labels,
},
ManifestOptions: agent.ManifestOptions{
AgentEnvVars: cluster.Spec.AgentEnvVars,
CheckinInterval: cfg.AgentCheckinInterval.Duration.String(),
Generation: string(cluster.UID) + "-" + strconv.FormatInt(cluster.Generation, 10),
PrivateRepoURL: cluster.Spec.PrivateRepoURL,
AgentEnvVars: cluster.Spec.AgentEnvVars,
AgentTolerations: cluster.Spec.AgentTolerations,
CheckinInterval: cfg.AgentCheckinInterval.Duration.String(),
Generation: string(cluster.UID) + "-" + strconv.FormatInt(cluster.Generation, 10),
PrivateRepoURL: cluster.Spec.PrivateRepoURL,
},
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/manageagent/manageagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (h *handler) newAgentBundle(ns string, cluster *fleet.Cluster) (runtime.Obj
AgentEnvVars: cluster.Spec.AgentEnvVars,
AgentImage: cfg.AgentImage,
AgentImagePullPolicy: cfg.AgentImagePullPolicy,
AgentTolerations: cluster.Spec.AgentTolerations,
CheckinInterval: cfg.AgentCheckinInterval.Duration.String(),
Generation: "bundle",
PrivateRepoURL: cluster.Spec.PrivateRepoURL,
Expand Down