Skip to content

Commit

Permalink
feat: handle enabling networking agents when aggregating condition (#231
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zhiying-lin committed Aug 11, 2022
1 parent c8dcdd5 commit 9df1982
Show file tree
Hide file tree
Showing 7 changed files with 943 additions and 165 deletions.
23 changes: 6 additions & 17 deletions apis/v1alpha1/internalmembercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,17 @@ func (m *InternalMemberCluster) GetConditionWithType(agentType AgentType, condit
// GetAgentStatus is used to retrieve agent status from internal member cluster,
// if it doesn't exist it creates the expected agent status and returns it.
func (m *InternalMemberCluster) GetAgentStatus(agentType AgentType) *AgentStatus {
// TODO: Refactor method
var desiredAgentStatus AgentStatus
for _, agentStatus := range m.Status.AgentStatus {
if agentStatus.Type == agentType {
desiredAgentStatus = agentStatus
}
}

if desiredAgentStatus.Type == "" {
desiredAgentStatus = AgentStatus{
Type: MemberAgent,
Conditions: []metav1.Condition{},
}
m.Status.AgentStatus = append(m.Status.AgentStatus, desiredAgentStatus)
}

for i := range m.Status.AgentStatus {
if m.Status.AgentStatus[i].Type == agentType {
return &m.Status.AgentStatus[i]
}
}
return nil
agentStatus := AgentStatus{
Type: agentType,
Conditions: []metav1.Condition{},
}
m.Status.AgentStatus = append(m.Status.AgentStatus, agentStatus)
return &m.Status.AgentStatus[len(m.Status.AgentStatus)-1]
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion docker/hub-agent.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the hubagent binary
FROM golang:1.17 as builder
FROM golang:1.18 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
2 changes: 1 addition & 1 deletion docker/member-agent.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the memberagent binary
FROM golang:1.17 as builder
FROM golang:1.18 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
2 changes: 1 addition & 1 deletion docker/refresh-token.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the hubagent binary
FROM golang:1.17 as builder
FROM golang:1.18 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
43 changes: 30 additions & 13 deletions pkg/controllers/membercluster/membercluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ const (
// Reconciler reconciles a MemberCluster object
type Reconciler struct {
client.Client
recorder record.EventRecorder
NetworkingAgentsEnabled bool // if networking agents are enabled, need to handle unjoin before leave
numberOfAgents int
recorder record.EventRecorder
// Need to update MC based on the IMC conditions based on the agent list.
NetworkingAgentsEnabled bool
// agents are used as hashset to query the expected agent type, so the value will be ignored.
agents map[fleetv1alpha1.AgentType]string
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -362,19 +364,32 @@ func (r *Reconciler) updateMemberClusterStatus(ctx context.Context, mc *fleetv1a
// aggregateJoinedCondition is used to calculate and mark the joined or left status for member cluster based on join conditions from all agents.
func (r *Reconciler) aggregateJoinedCondition(mc *fleetv1alpha1.MemberCluster) {
klog.V(5).InfoS("syncJoinedCondition", "memberCluster", klog.KObj(mc))
// TODO: Fix condition
if len(mc.Status.AgentStatus) < r.numberOfAgents {
if len(mc.Status.AgentStatus) < len(r.agents) {
markMemberClusterUnknown(r.recorder, mc)
return
}
joined := true
left := true
reportedAgents := make(map[fleetv1alpha1.AgentType]bool)
for _, agentStatus := range mc.Status.AgentStatus {
conditions := agentStatus.Conditions
condition := meta.FindStatusCondition(conditions, string(fleetv1alpha1.AgentJoined))
if condition != nil {
joined = joined && condition.Status == metav1.ConditionTrue
left = left && condition.Status == metav1.ConditionFalse
if _, found := r.agents[agentStatus.Type]; !found {
klog.V(2).InfoS("Ignoring unexpected agent type status", "agentStatus", agentStatus)
continue // ignore any unexpected agent type
}
condition := meta.FindStatusCondition(agentStatus.Conditions, string(fleetv1alpha1.AgentJoined))
if condition == nil {
markMemberClusterUnknown(r.recorder, mc)
return
}

joined = joined && condition.Status == metav1.ConditionTrue
left = left && condition.Status == metav1.ConditionFalse
reportedAgents[agentStatus.Type] = true
}

if len(reportedAgents) < len(r.agents) {
markMemberClusterUnknown(r.recorder, mc)
return
}

if joined && !left {
Expand Down Expand Up @@ -471,10 +486,12 @@ func markMemberClusterUnknown(recorder record.EventRecorder, mc apis.Conditioned
// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
r.recorder = mgr.GetEventRecorderFor("memberCluster")
r.agents = make(map[fleetv1alpha1.AgentType]string)
r.agents[fleetv1alpha1.MemberAgent] = ""

if r.NetworkingAgentsEnabled {
r.numberOfAgents = 3
} else {
r.numberOfAgents = 1
r.agents[fleetv1alpha1.MultiClusterServiceAgent] = ""
r.agents[fleetv1alpha1.ServiceExportImportAgent] = ""
}
return ctrl.NewControllerManagedBy(mgr).
For(&fleetv1alpha1.MemberCluster{}).
Expand Down
Loading

0 comments on commit 9df1982

Please sign in to comment.