Skip to content

Commit

Permalink
Add hostNetwork upgrade change reconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil-Grigorev authored and manno committed Jul 23, 2024
1 parent 9be4674 commit 5d8ffad
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5529,6 +5529,11 @@ spec:
used to detect changes.
nullable: true
type: string
agentHostNetwork:
description: AgentHostNetwork defines observed state of spec.hostNetwork
setting that is currently used.
nullable: true
type: boolean
agentMigrated:
description: 'AgentMigrated is always set to true after importing
a cluster. If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package manageagent

import (
"cmp"
"context"
"crypto/sha256"
"encoding/json"
Expand All @@ -28,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
)

const (
Expand Down Expand Up @@ -169,6 +171,11 @@ func (h *handler) updateClusterStatus(cluster *fleet.Cluster, status fleet.Clust
changed = true
}

if hostNetwork := *cmp.Or(cluster.Spec.HostNetwork, ptr.To(false)); status.AgentHostNetwork != hostNetwork {
status.AgentHostNetwork = hostNetwork
changed = true
}

if c, hash, err := hashChanged(cluster.Spec.AgentAffinity, status.AgentAffinityHash); err != nil {
return status, changed, err
} else if c {
Expand Down Expand Up @@ -263,6 +270,7 @@ func (h *handler) newAgentBundle(ns string, cluster *fleet.Cluster) (runtime.Obj
SystemDefaultRegistry: cfg.SystemDefaultRegistry,
AgentAffinity: cluster.Spec.AgentAffinity,
AgentResources: cluster.Spec.AgentResources,
HostNetwork: *cmp.Or(cluster.Spec.HostNetwork, ptr.To(false)),
},
)
agentYAML, err := yaml.Export(objs...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rancher/wrangler/v3/pkg/generic/fake"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
)
Expand Down Expand Up @@ -271,3 +272,59 @@ func TestOnClusterChangeTolerations(t *testing.T) {
})
}
}

func TestOnClusterChangeHostNetwork(t *testing.T) {
ctrl := gomock.NewController(t)
namespaces := fake.NewMockNonNamespacedControllerInterface[*corev1.Namespace, *corev1.NamespaceList](ctrl)
h := &handler{namespaces: namespaces}

for _, tt := range []struct {
name string
cluster *fleet.Cluster
status fleet.ClusterStatus
expectedStatus fleet.ClusterStatus
enqueues int
}{
{
name: "Empty",
cluster: &fleet.Cluster{},
status: fleet.ClusterStatus{},
expectedStatus: fleet.ClusterStatus{},
enqueues: 0,
},
{
name: "Equal HostNetwork",
cluster: &fleet.Cluster{Spec: fleet.ClusterSpec{HostNetwork: ptr.To(true)}},
status: fleet.ClusterStatus{AgentHostNetwork: true},
expectedStatus: fleet.ClusterStatus{AgentHostNetwork: true},
enqueues: 0,
},
{
name: "Changed HostNetwork",
cluster: &fleet.Cluster{Spec: fleet.ClusterSpec{HostNetwork: ptr.To(true)}},
status: fleet.ClusterStatus{AgentHostNetwork: false},
expectedStatus: fleet.ClusterStatus{AgentHostNetwork: true},
enqueues: 1,
},
{
name: "Removed Resources",
cluster: &fleet.Cluster{Spec: fleet.ClusterSpec{}},
status: fleet.ClusterStatus{AgentHostNetwork: true},
expectedStatus: fleet.ClusterStatus{AgentHostNetwork: false},
enqueues: 1,
},
} {
t.Run(tt.name, func(t *testing.T) {
namespaces.EXPECT().Enqueue(gomock.Any()).Times(tt.enqueues)

status, err := h.onClusterStatusChange(tt.cluster, tt.status)
if err != nil {
t.Error(err)
}

if status.AgentHostNetwork != tt.expectedStatus.AgentHostNetwork {
t.Fatalf("agent hostStatus is not equal: %v vs %v", status.AgentHostNetwork, tt.expectedStatus.AgentHostNetwork)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ type ClusterStatus struct {
// AgentPrivateRepoURL is the private repo URL for the agent that is currently used.
// +nullable
AgentPrivateRepoURL string `json:"agentPrivateRepoURL,omitempty"`
// AgentHostNetwork defines observed state of spec.hostNetwork setting that is currently used.
// +nullable
AgentHostNetwork bool `json:"agentHostNetwork,omitempty"`
// AgentDeployedGeneration is the generation of the agent that is currently deployed.
// +nullable
AgentDeployedGeneration *int64 `json:"agentDeployedGeneration,omitempty"`
Expand Down

0 comments on commit 5d8ffad

Please sign in to comment.