Skip to content

Commit

Permalink
Add hostNetwork setting for agent deployment policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil-Grigorev authored Jul 22, 2024
1 parent 45e116e commit 4505649
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions charts/fleet-agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ spec:
- name: kube
emptyDir: {}
serviceAccountName: fleet-agent
{{- if .Values.fleetAgent.hostNetwork }}
hostNetwork: true
{{- end }}
nodeSelector: {{ include "linux-node-selector" . | nindent 8 }}
{{- if .Values.fleetAgent.nodeSelector }}
{{ toYaml .Values.fleetAgent.nodeSelector | indent 8 }}
Expand Down
3 changes: 3 additions & 0 deletions charts/fleet-agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ fleetAgent:
nodeSelector: {}
## List of node taints to tolerate (requires Kubernetes >= 1.6)
tolerations: []
## HostNetwork setting for the agent deployment.
## When set allows for provisioning of network related bundles (CNI configuration) in a cluster without CNI.
hostNetwork: false
kubectl:
## Node labels for pod assignment
## Ref: https://kubernetes.io/docs/user-guide/node-selection/
Expand Down
7 changes: 7 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5440,6 +5440,13 @@ spec:
either be predefined, or generated when importing the cluster.'
nullable: true
type: string
hostNetwork:
description: 'HostNetwork sets the agent StatefulSet to use hostNetwork:
true setting.
Allows for provisioning of network related bundles (CNI configuration).'
nullable: true
type: boolean
kubeConfigSecret:
description: 'KubeConfigSecret is the name of the secret containing
the kubeconfig for the downstream cluster.
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/controller/agentmanagement/agent/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ManifestOptions struct {
SystemDefaultRegistry string
AgentAffinity *corev1.Affinity
AgentResources *corev1.ResourceRequirements
HostNetwork bool
}

// Manifest builds and returns a deployment manifest for the fleet-agent with a
Expand Down Expand Up @@ -298,6 +299,9 @@ func agentApp(namespace string, agentScope string, opts ManifestOptions) *appsv1
// additional tolerations from cluster
app.Spec.Template.Spec.Tolerations = append(app.Spec.Template.Spec.Tolerations, opts.AgentTolerations...)

// Set hostNetwork
app.Spec.Template.Spec.HostNetwork = opts.HostNetwork

// overwrite affinity if present on cluster
if opts.AgentAffinity != nil {
app.Spec.Template.Spec.Affinity = opts.AgentAffinity
Expand Down
48 changes: 48 additions & 0 deletions internal/cmd/controller/agentmanagement/agent/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,54 @@ func TestManifestAgentTolerations(t *testing.T) {
}
}

func TestManifestAgentHostNetwork(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",
PrivateRepoURL: "private.rancher.com:5000",
SystemDefaultRegistry: "default.rancher.com",
}

for _, testCase := range []struct {
name string
getOpts func() ManifestOptions
expectedNetwork bool
}{
{
name: "DefaultSetting",
getOpts: func() ManifestOptions {
return baseOpts
},
expectedNetwork: false,
},
{
name: "With hostNetwork",
getOpts: func() ManifestOptions {
withHostNetwork := baseOpts
withHostNetwork.HostNetwork = true
return withHostNetwork
},
expectedNetwork: true,
},
} {
t.Run(testCase.name, func(t *testing.T) {
agent := getAgentFromManifests(namespace, scope, testCase.getOpts())
if agent == nil {
t.Fatal("there were no deployments returned from the manifests")
}

if !cmp.Equal(agent.Spec.Template.Spec.HostNetwork, testCase.expectedNetwork) {
t.Fatalf("hostNetwork is not as expected: %v", agent.Spec.Template.Spec.HostNetwork)
}
})
}
}

func TestManifestAgentAffinity(t *testing.T) {
const namespace = "fleet-system"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cluster

import (
"cmp"
"context"
"crypto/sha256"
"encoding/json"
Expand Down Expand Up @@ -32,6 +33,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/utils/ptr"
)

var (
Expand Down Expand Up @@ -323,6 +325,7 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
PrivateRepoURL: cluster.Spec.PrivateRepoURL,
AgentAffinity: cluster.Spec.AgentAffinity,
AgentResources: cluster.Spec.AgentResources,
HostNetwork: *cmp.Or(cluster.Spec.HostNetwork, ptr.To(false)),
},
})
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ type ClusterSpec struct {
// +nullable
// AgentResources sets the resources for the cluster's agent deployment.
AgentResources *corev1.ResourceRequirements `json:"agentResources,omitempty"`

// +nullable
// +optional
// HostNetwork sets the agent StatefulSet to use hostNetwork: true setting.
// Allows for provisioning of network related bundles (CNI configuration).
HostNetwork *bool `json:"hostNetwork,omitempty"`
}

type ClusterStatus struct {
Expand Down
5 changes: 5 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.

0 comments on commit 4505649

Please sign in to comment.