Skip to content

Commit

Permalink
Use sync.Map
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak committed Nov 29, 2023
1 parent ba6d777 commit b6bb476
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 55 deletions.
17 changes: 0 additions & 17 deletions controllers/flowcollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,6 @@ func Start(ctx context.Context, mgr *manager.Manager) error {
return nil
}

//+kubebuilder:rbac:groups=apps,resources=deployments;daemonsets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=namespaces;services;serviceaccounts;configmaps;secrets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=endpoints,verbs=get;list;watch
//+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings;clusterroles;rolebindings;roles,verbs=get;list;create;delete;update;watch
//+kubebuilder:rbac:groups=console.openshift.io,resources=consoleplugins,verbs=get;create;delete;update;patch;list;watch
//+kubebuilder:rbac:groups=operator.openshift.io,resources=consoles,verbs=get;update;list;update;watch
//+kubebuilder:rbac:groups=flows.netobserv.io,resources=flowcollectors,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=flows.netobserv.io,resources=flowcollectors/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=flows.netobserv.io,resources=flowcollectors/finalizers,verbs=update
//+kubebuilder:rbac:groups=security.openshift.io,resources=securitycontextconstraints,resourceNames=hostnetwork,verbs=use
//+kubebuilder:rbac:groups=security.openshift.io,resources=securitycontextconstraints,verbs=list;create;update;watch
//+kubebuilder:rbac:groups=apiregistration.k8s.io,resources=apiservices,verbs=list;get;watch
//+kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;prometheusrules,verbs=get;create;delete;update;patch;list;watch
//+kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions,verbs=get;list;watch
//+kubebuilder:rbac:groups=loki.grafana.com,resources=network,resourceNames=logs,verbs=get;create
//+kubebuilder:rbac:urls="/metrics",verbs=get

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// Modify the Reconcile function to compare the state specified by
Expand Down
56 changes: 20 additions & 36 deletions pkg/manager/status/status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,95 +27,79 @@ const (
var allNames = []ComponentName{FlowCollectorLegacy, Monitoring}

type Manager struct {
sync.Mutex

statuses map[ComponentName]ComponentStatus
statuses sync.Map
}

func NewManager() *Manager {
s := Manager{
statuses: make(map[ComponentName]ComponentStatus, len(allNames)),
}
s := Manager{}
for _, cpnt := range allNames {
s.statuses[cpnt] = ComponentStatus{
s.statuses.Store(cpnt, ComponentStatus{
name: cpnt,
status: StatusUnknown,
}
})
}
return &s
}

func (s *Manager) setInProgress(cpnt ComponentName, reason, message string) {
s.Lock()
defer s.Unlock()
s.statuses[cpnt] = ComponentStatus{
s.statuses.Store(cpnt, ComponentStatus{
name: cpnt,
status: StatusInProgress,
reason: reason,
message: message,
}
})
}

func (s *Manager) setFailure(cpnt ComponentName, reason, message string) {
s.Lock()
defer s.Unlock()
s.statuses[cpnt] = ComponentStatus{
s.statuses.Store(cpnt, ComponentStatus{
name: cpnt,
status: StatusFailure,
reason: reason,
message: message,
}
})
}

func (s *Manager) hasFailure(cpnt ComponentName) bool {
s.Lock()
defer s.Unlock()
v := s.statuses[cpnt]
return v.status == StatusFailure
v, _ := s.statuses.Load(cpnt)
return v != nil && v.(ComponentStatus).status == StatusFailure
}

func (s *Manager) setReady(cpnt ComponentName) {
s.Lock()
defer s.Unlock()
s.statuses[cpnt] = ComponentStatus{
s.statuses.Store(cpnt, ComponentStatus{
name: cpnt,
status: StatusReady,
}
})
}

func (s *Manager) setUnknown(cpnt ComponentName) {
s.Lock()
defer s.Unlock()
s.statuses[cpnt] = ComponentStatus{
s.statuses.Store(cpnt, ComponentStatus{
name: cpnt,
status: StatusUnknown,
}
})
}

func (s *Manager) setUnused(cpnt ComponentName) {
s.Lock()
defer s.Unlock()
s.statuses[cpnt] = ComponentStatus{
s.statuses.Store(cpnt, ComponentStatus{
name: cpnt,
status: StatusUnknown,
reason: "ComponentUnused",
}
})
}

func (s *Manager) getConditions() []metav1.Condition {
s.Lock()
defer s.Unlock()
global := metav1.Condition{
Type: "Ready",
Status: metav1.ConditionTrue,
Reason: "Ready",
}
conds := []metav1.Condition{}
counters := make(map[Status]int, len(allNames))
for _, status := range s.statuses {
s.statuses.Range(func(_, v any) bool {
status := v.(ComponentStatus)
conds = append(conds, status.toCondition())
counters[status.status]++
}
return true
})
global.Message = fmt.Sprintf("%d ready components, %d with failure, %d pending", counters[StatusReady], counters[StatusFailure], counters[StatusInProgress])
if counters[StatusFailure] > 0 {
global.Status = metav1.ConditionFalse
Expand Down
2 changes: 0 additions & 2 deletions pkg/narrowcache/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ type handlerOnQueue struct {
}

func (c *Client) Get(ctx context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
rlog := log.FromContext(ctx).WithName("narrowcache")
rlog.WithValues("key", key).Info("Getting object:")
gvk, err := c.GroupVersionKindFor(out)
if err != nil {
return err
Expand Down

0 comments on commit b6bb476

Please sign in to comment.