Skip to content

Commit

Permalink
updated quickstart, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Nov 29, 2023
1 parent 498b1d1 commit 3982b91
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 272 deletions.
2 changes: 0 additions & 2 deletions api/crds/kubefox.xigxog.io_clustervirtualenvs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ spec:
type: string
type: object
type: object
required:
- spec
type: object
served: true
storage: true
2 changes: 0 additions & 2 deletions api/crds/kubefox.xigxog.io_virtualenvs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ spec:
type: string
type: object
type: object
required:
- spec
type: object
served: true
storage: true
2 changes: 0 additions & 2 deletions api/crds/kubefox.xigxog.io_virtualenvsnapshots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ spec:
type: string
type: object
type: object
required:
- spec
type: object
served: true
storage: true
2 changes: 1 addition & 1 deletion api/kubernetes/v1alpha1/cluster_env_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ClusterVirtualEnv struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClusterEnvSpec `json:"spec"`
Spec ClusterEnvSpec `json:"spec,omitempty"`
Data EnvData `json:"data,omitempty"`
Details EnvDetails `json:"details,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion api/kubernetes/v1alpha1/env_snapshot_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type VirtualEnvSnapshot struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClusterEnvSpec `json:"spec"`
Spec ClusterEnvSpec `json:"spec,omitempty"`
Data EnvSnapshotData `json:"data,omitempty"`
Details EnvDetails `json:"details,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion api/kubernetes/v1alpha1/env_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type VirtualEnv struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec EnvSpec `json:"spec"`
Spec EnvSpec `json:"spec,omitempty"`
Data EnvData `json:"data,omitempty"`
Details EnvDetails `json:"details,omitempty"`
}
Expand Down
3 changes: 2 additions & 1 deletion api/kubernetes/v1alpha1/release_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type ReleaseStatusEntry struct {
}

type ReleaseStatus struct {
Current *ReleaseStatusEntry `json:"current,omitempty"`
// +kubebuilder:validation:Optional
Current *ReleaseStatusEntry `json:"current"`
Requested *ReleaseStatusEntry `json:"requested,omitempty"`

History []ReleaseStatusEntry `json:"history,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions components/broker/engine/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func (str *Store) DeploymentMatcher(ctx context.Context, evtCtx *kubefox.EventCo

func (str *Store) OnAdd(obj interface{}, isInInitialList bool) {
if isInInitialList {
str.log.Debugf("%T initialized", obj)
return
}
str.onChange(obj, "added")
Expand Down
8 changes: 2 additions & 6 deletions components/operator/controller/component_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (cm *ComponentManager) ReconcileApps(ctx context.Context, namespace string)
return false, k8s.IgnoreNotFound(err)
}
if !platform.Status.Available {
cm.log.Debug("Platform not available")
return false, nil
}

Expand All @@ -116,11 +117,6 @@ func (cm *ComponentManager) ReconcileApps(ctx context.Context, namespace string)
logkf.KeyPlatform, platform.Name,
)

if !platform.Status.Available {
log.Debug("Platform not available")
return false, nil
}

appDepList := &v1alpha1.AppDeploymentList{}
if err := cm.List(ctx, appDepList, client.InNamespace(platform.Namespace)); err != nil {
return false, err
Expand Down Expand Up @@ -202,7 +198,7 @@ func (cm *ComponentManager) ReconcileApps(ctx context.Context, namespace string)
}
}

log.Debugf("AppDeployment '%s.%s'; available: %t", d.Name, d.Namespace, available)
log.Debugf("AppDeployment '%s/%s'; available: %t", d.Namespace, d.Name, available)
}

for _, d := range compDepList.Items {
Expand Down
4 changes: 2 additions & 2 deletions components/operator/controller/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *AppDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
"namespace", req.Namespace,
"name", req.Name,
)
log.Debugf("reconciling AppDeployment '%s.%s'", req.Name, req.Namespace)
log.Debugf("reconciling AppDeployment '%s/%s'", req.Namespace, req.Name)

if err := r.reconcile(ctx, req, log); err != nil {
if IsFailedWebhookErr(err) {
Expand All @@ -53,7 +53,7 @@ func (r *AppDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, err
}

log.Debugf("reconciling AppDeployment '%s.%s' done", req.Name, req.Namespace)
log.Debugf("reconciling AppDeployment '%s/%s' done", req.Namespace, req.Name)

return ctrl.Result{}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions components/operator/controller/env_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *SnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
"namespace", req.Namespace,
"name", req.Name,
)
log.Debugf("reconciling VirtualEnvSnapshot '%s.%s'", req.Name, req.Namespace)
log.Debugf("reconciling VirtualEnvSnapshot '%s/%s'", req.Namespace, req.Name)

if err := r.reconcile(ctx, req, log); err != nil {
if IsFailedWebhookErr(err) {
Expand All @@ -52,7 +52,7 @@ func (r *SnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}

log.Debugf("reconciling VirtualEnvSnapshot '%s.%s' done", req.Name, req.Namespace)
log.Debugf("reconciling VirtualEnvSnapshot '%s/%s' done", req.Namespace, req.Name)

return ctrl.Result{}, nil
}
Expand Down
85 changes: 46 additions & 39 deletions components/operator/controller/platform_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,24 @@ func (r *PlatformReconciler) SetupWithManager(mgr ctrl.Manager) error {

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.

func (r *PlatformReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.log.With(
"namespace", req.Namespace,
"name", req.Name,
)
log.Debugf("reconciling Platform '%s.%s'", req.Name, req.Namespace)
log.Debugf("reconciling Platform '%s/%s'", req.Namespace, req.Name)

p, err := r.reconcile(ctx, req, log)
p, ready, err := r.reconcile(ctx, req, log)
if p != nil {
curStatus := p.Status

p.Status.Available = (err == nil)
p.Status.Available = ready && (err == nil)
if err := r.updateComponentsStatus(ctx, p); err != nil {
r.log.Error(err)
}

if !k8s.DeepEqual(curStatus, p.Status) {
if err := r.Status().Update(ctx, p); err != nil {
if err := r.ApplyStatus(ctx, p); err != nil {
r.log.Error(err)
}
}
Expand All @@ -99,33 +98,35 @@ func (r *PlatformReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
}

log.Debugf("reconciling Platform '%s.%s' done", req.Name, req.Namespace)
log.Debugf("reconciling Platform '%s/%s' done", req.Namespace, req.Name)

return ctrl.Result{}, err
}

func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, log *logkf.Logger) (*v1alpha1.Platform, error) {
func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, log *logkf.Logger) (*v1alpha1.Platform, bool, error) {
pKey := fmt.Sprintf("%s/%s", req.Namespace, req.Name)

ns := &v1.Namespace{}
if err := r.Get(ctx, k8s.Key("", req.Namespace), ns); err != nil {
if k8s.IsNotFound(err) {
log.Debug("Namespace is gone")
return nil, nil
}
return nil, log.ErrorN("unable to fetch Namespace: %w", err)
r.setSetup(pKey, false)
return nil, false, k8s.IgnoreNotFound(err)
}
if ns.Status.Phase == v1.NamespaceTerminating {
log.Debug("Namespace is terminating")
return nil, nil
r.setSetup(pKey, false)
return nil, false, nil
}

p := &v1alpha1.Platform{}
if err := r.Get(ctx, req.NamespacedName, p); err != nil {
return nil, err
r.setSetup(pKey, false)
return nil, false, k8s.IgnoreNotFound(err)
}

cm := &v1.ConfigMap{}
if err := r.Get(ctx, k8s.Key(r.Namespace, r.Instance+"-root-ca"), cm); err != nil {
return p, log.ErrorN("unable to fetch root CA configmap: %w", err)
r.setSetup(pKey, false)
return p, false, log.ErrorN("unable to fetch root CA configmap: %w", err)
}

maxEventSize := p.Spec.Events.MaxSize.Value()
Expand Down Expand Up @@ -156,33 +157,29 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
},
}

r.mutex.Lock()
setup := r.setupMap[baseTD.PlatformFullName()]
r.mutex.Unlock()
setup := r.isSetup(pKey)
if setup {
r.log.Debugf("Platform '%s' already setup ", baseTD.PlatformFullName())
r.log.Debugf("Platform '%s' already setup ", pKey)

} else {
// Ensure there are valid commits for Platform components.
if !api.RegexpCommit.MatchString(build.Info.BrokerCommit) ||
!api.RegexpCommit.MatchString(build.Info.HTTPSrvCommit) {
return p, log.ErrorN("broker or httpsrv commit from build info is invalid")
return p, false, log.ErrorN("broker or httpsrv commit from build info is invalid")
}
if err := r.setupVault(ctx, baseTD); err != nil {
return p, log.ErrorN("problem setting up vault: %w", err)
return p, false, log.ErrorN("problem setting up vault: %w", err)
}
if err := r.ApplyTemplate(ctx, "platform", &baseTD.Data, log); err != nil {
return p, log.ErrorN("problem setting up Platform: %w", err)
return p, false, log.ErrorN("problem setting up Platform: %w", err)
}

r.mutex.Lock()
r.setupMap[baseTD.PlatformFullName()] = true
r.mutex.Unlock()
r.setSetup(pKey, true)
}

if r.setDefaults(p) {
log.Debug("Platform defaults set, persisting")
return nil, r.Update(ctx, p)
return nil, false, r.Update(ctx, p)
}

td := baseTD.ForComponent(api.PlatformComponentNATS, &appsv1.StatefulSet{}, &NATSDefaults, templates.Component{
Expand All @@ -193,11 +190,11 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
IsPlatformComponent: true,
})
if err := r.setupVaultComponent(ctx, td, ""); err != nil {
return p, err
return p, false, err
}
if rdy, err := r.CompMgr.SetupComponent(ctx, td); !rdy || err != nil {
chill()
return p, err
return p, rdy, err
}

td = baseTD.ForComponent(api.PlatformComponentBroker, &appsv1.DaemonSet{}, &BrokerDefaults, templates.Component{
Expand All @@ -209,11 +206,11 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
IsPlatformComponent: true,
})
if err := r.setupVaultComponent(ctx, td, ""); err != nil {
return p, err
return p, false, err
}
if rdy, err := r.CompMgr.SetupComponent(ctx, td); !rdy || err != nil {
chill()
return p, err
return p, rdy, err
}

td = baseTD.ForComponent(api.PlatformComponentHTTPSrv, &appsv1.Deployment{}, &HTTPSrvDefaults, templates.Component{
Expand All @@ -228,14 +225,14 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
td.Values["httpPort"] = p.Spec.HTTPSrv.Service.Ports.HTTP
td.Values["httpsPort"] = p.Spec.HTTPSrv.Service.Ports.HTTPS
if err := r.setupVaultComponent(ctx, td, ""); err != nil {
return p, err
return p, false, err
}
if rdy, err := r.CompMgr.SetupComponent(ctx, td); !rdy || err != nil {
chill()
return p, err
return p, rdy, err
}

return p, nil
return p, true, nil
}

func (r *PlatformReconciler) updateComponentsStatus(ctx context.Context, p *v1alpha1.Platform) error {
Expand Down Expand Up @@ -321,9 +318,7 @@ func (r *PlatformReconciler) setupVault(ctx context.Context, td *TemplateData) e
}

func (r *PlatformReconciler) setupVaultComponent(ctx context.Context, td *TemplateData, additionalPolicies string) error {
r.mutex.Lock()
setup := r.setupMap[td.ComponentFullName()]
r.mutex.Unlock()
setup := r.isSetup(td.ComponentFullName())
if setup {
r.log.Debugf("vault already setup for component '%s'", td.ComponentFullName())
return nil
Expand Down Expand Up @@ -376,9 +371,7 @@ func (r *PlatformReconciler) setupVaultComponent(ctx context.Context, td *Templa
return err
}

r.mutex.Lock()
r.setupMap[td.ComponentFullName()] = true
r.mutex.Unlock()
r.setSetup(td.ComponentFullName(), true)
r.log.Debugf("vault successfully setup for component '%s'", td.ComponentFullName())

return nil
Expand Down Expand Up @@ -467,6 +460,20 @@ func (r *PlatformReconciler) setDefaults(platform *v1alpha1.Platform) bool {
return !k8s.DeepEqual(cur, s)
}

func (r *PlatformReconciler) setSetup(key string, val bool) {
r.mutex.Lock()
defer r.mutex.Unlock()

r.setupMap[key] = val
}

func (r *PlatformReconciler) isSetup(key string) bool {
r.mutex.Lock()
defer r.mutex.Unlock()

return r.setupMap[key]
}

// chill waits a few seconds for things to chillax.
func chill() {
time.Sleep(time.Second * 3)
Expand Down
12 changes: 8 additions & 4 deletions components/operator/controller/release_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func (r *ReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
"namespace", req.Namespace,
"name", req.Name,
)
log.Debugf("reconciling Release '%s.%s'", req.Name, req.Namespace)
log.Debugf("reconciling Release '%s/%s'", req.Namespace, req.Name)

result, err := r.reconcile(ctx, req, log)
if IsFailedWebhookErr(err) {
log.Debug("reconcile failed because of webhook, retrying in 15 seconds")
return ctrl.Result{RequeueAfter: time.Second * 15}, nil
}

log.Debugf("reconciling Release '%s.%s' done", req.Name, req.Namespace)
log.Debugf("reconciling Release '%s/%s' done", req.Namespace, req.Name)

return result, err
}
Expand All @@ -82,7 +82,7 @@ func (r *ReleaseReconciler) reconcile(ctx context.Context, req ctrl.Request, log
return ctrl.Result{}, err
}
if k8s.IsNotFound(err) || rel.ResourceVersion == "0" {
log.Debug("Release deleted")
log.Debugf("Release '%s/%s does not exists'", req.Namespace, req.Name)
return ctrl.Result{}, nil
}
if rel.DeletionTimestamp != nil {
Expand Down Expand Up @@ -333,7 +333,11 @@ func (r *ReleaseReconciler) watchClusterVirtualEnv(ctx context.Context, env clie
}

func (r *ReleaseReconciler) watchVirtualEnv(ctx context.Context, env client.Object) []reconcile.Request {
return []reconcile.Request{{NamespacedName: k8s.Key(env.GetNamespace(), env.GetName())}}
return r.findReleases(ctx,
client.MatchingLabels{
api.LabelK8sVirtualEnv: env.GetName(),
},
)
}

func (r *ReleaseReconciler) watchVirtualEnvSnapshot(ctx context.Context, env client.Object) []reconcile.Request {
Expand Down
Loading

0 comments on commit 3982b91

Please sign in to comment.