Skip to content

Commit

Permalink
switched to patch instead of replace for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Nov 30, 2023
1 parent 7db2230 commit affcd81
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 102 deletions.
6 changes: 6 additions & 0 deletions api/crds/kubefox.xigxog.io_releases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ spec:
requestTime:
format: date-time
type: string
virtualEnvSnapshot:
type: string
required:
- appDeployment
type: object
Expand Down Expand Up @@ -119,6 +121,8 @@ spec:
requestTime:
format: date-time
type: string
virtualEnvSnapshot:
type: string
required:
- appDeployment
type: object
Expand Down Expand Up @@ -148,6 +152,8 @@ spec:
requestTime:
format: date-time
type: string
virtualEnvSnapshot:
type: string
required:
- appDeployment
type: object
Expand Down
17 changes: 10 additions & 7 deletions api/kubernetes/v1alpha1/release_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

type ReleaseSpec struct {
AppDeployment ReleaseAppDeployment `json:"appDeployment"`
VirtualEnvSnapshot string `json:"virtualEnvSnapshot,omitempty"`
AppDeployment ReleaseAppDeployment `json:"appDeployment"`
// +kubebuilder:validation:Optional
VirtualEnvSnapshot string `json:"virtualEnvSnapshot"`
HistoryLimit *ReleaseHistoryLimit `json:"historyLimit,omitempty"`
}

Expand All @@ -24,7 +25,8 @@ type ReleaseAppDeployment struct {
// Version of the App being released. Use of semantic versioning is
// recommended. If set the value is compared to the AppDeployment version.
// If the two versions do not match the release will fail.
Version string `json:"version,omitempty"`
// +kubebuilder:validation:Optional
Version string `json:"version"`
}

type ReleaseHistoryLimit struct {
Expand All @@ -36,10 +38,11 @@ type ReleaseHistoryLimit struct {
}

type ReleaseStatusEntry struct {
AppDeployment ReleaseAppDeployment `json:"appDeployment"`
RequestTime metav1.Time `json:"requestTime,omitempty"`
AvailableTime *metav1.Time `json:"availableTime,omitempty"`
ArchiveTime *metav1.Time `json:"archiveTime,omitempty"`
AppDeployment ReleaseAppDeployment `json:"appDeployment"`
VirtualEnvSnapshot string `json:"virtualEnvSnapshot,omitempty"`
RequestTime metav1.Time `json:"requestTime,omitempty"`
AvailableTime *metav1.Time `json:"availableTime,omitempty"`
ArchiveTime *metav1.Time `json:"archiveTime,omitempty"`
}

type ReleaseStatus struct {
Expand Down
14 changes: 6 additions & 8 deletions components/operator/controller/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ func (r *AppDeploymentReconciler) reconcile(ctx context.Context, req ctrl.Reques
if err := r.Get(ctx, req.NamespacedName, appDep); err != nil {
return k8s.IgnoreNotFound(err)
}
curAppDep := appDep.DeepCopy()

k8s.UpdateLabel(appDep, api.LabelK8sAppVersion, appDep.Spec.Version)
k8s.UpdateLabel(appDep, api.LabelK8sAppCommit, appDep.Spec.App.Commit)
k8s.UpdateLabel(appDep, api.LabelK8sAppCommitShort, utils.ShortCommit(appDep.Spec.App.Commit))
k8s.UpdateLabel(appDep, api.LabelK8sAppTag, appDep.Spec.App.Tag)
k8s.UpdateLabel(appDep, api.LabelK8sAppBranch, appDep.Spec.App.Branch)
if k8s.UpdateLabel(appDep, api.LabelK8sAppVersion, appDep.Spec.Version) ||
k8s.UpdateLabel(appDep, api.LabelK8sAppCommit, appDep.Spec.App.Commit) ||
k8s.UpdateLabel(appDep, api.LabelK8sAppCommitShort, utils.ShortCommit(appDep.Spec.App.Commit)) ||
k8s.UpdateLabel(appDep, api.LabelK8sAppTag, appDep.Spec.App.Tag) ||
k8s.UpdateLabel(appDep, api.LabelK8sAppBranch, appDep.Spec.App.Branch) {

if !k8s.DeepEqual(curAppDep.ObjectMeta, appDep.ObjectMeta) {
log.Debug("AppDeployment modified, updating")
return r.Update(ctx, appDep)
return r.Apply(ctx, appDep)
}

if _, err := r.CompMgr.ReconcileApps(ctx, req.Namespace); err != nil {
Expand Down
10 changes: 4 additions & 6 deletions components/operator/controller/env_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ func (r *SnapshotReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
if err := r.Get(ctx, req.NamespacedName, env); err != nil {
return k8s.IgnoreNotFound(err)
}
curAppDep := env.DeepCopy()

k8s.UpdateLabel(env, api.LabelK8sVirtualEnv, env.Data.Source.Name)
k8s.UpdateLabel(env, api.LabelK8sSourceKind, env.Data.Source.Kind)
k8s.UpdateLabel(env, api.LabelK8sSourceResourceVersion, env.Data.Source.ResourceVersion)
if k8s.UpdateLabel(env, api.LabelK8sVirtualEnv, env.Data.Source.Name) ||
k8s.UpdateLabel(env, api.LabelK8sSourceKind, env.Data.Source.Kind) ||
k8s.UpdateLabel(env, api.LabelK8sSourceResourceVersion, env.Data.Source.ResourceVersion) {

if !k8s.DeepEqual(curAppDep.ObjectMeta, env.ObjectMeta) {
log.Debug("VirtualEnvSnapshot modified, updating")
return r.Update(ctx, env)
return r.Apply(ctx, env)
}

return nil
Expand Down
107 changes: 53 additions & 54 deletions components/operator/controller/platform_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,16 @@ func (r *PlatformReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
)
log.Debugf("reconciling Platform '%s/%s'", req.Namespace, req.Name)

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

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

if !k8s.DeepEqual(curStatus, p.Status) {
if err := r.ApplyStatus(ctx, p); err != nil {
if !k8s.DeepEqual(platform.Status, orig.Status) {
if err := r.MergeStatus(ctx, platform, orig); err != nil {
r.log.Error(err)
}
}
Expand Down Expand Up @@ -117,20 +116,20 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
return nil, false, nil
}

p := &v1alpha1.Platform{}
if err := r.Get(ctx, req.NamespacedName, p); err != nil {
platform := &v1alpha1.Platform{}
if err := r.Get(ctx, req.NamespacedName, platform); err != nil {
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 {
r.setSetup(pKey, false)
return p, false, log.ErrorN("unable to fetch root CA configmap: %w", err)
return platform, false, log.ErrorN("unable to fetch root CA configmap: %w", err)
}

maxEventSize := p.Spec.Events.MaxSize.Value()
if p.Spec.Events.MaxSize.IsZero() {
maxEventSize := platform.Spec.Events.MaxSize.Value()
if platform.Spec.Events.MaxSize.IsZero() {
maxEventSize = api.DefaultMaxEventSizeBytes
}
baseTD := &TemplateData{
Expand All @@ -142,14 +141,14 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
BootstrapImage: BootstrapImage,
},
Platform: templates.Platform{
Name: p.Name,
Namespace: p.Namespace,
Name: platform.Name,
Namespace: platform.Namespace,
},
Owner: []*metav1.OwnerReference{
metav1.NewControllerRef(p, p.GroupVersionKind()),
metav1.NewControllerRef(platform, platform.GroupVersionKind()),
},
BuildInfo: build.Info,
Logger: p.Spec.Logger,
Logger: platform.Spec.Logger,
Values: map[string]any{
"maxEventSize": maxEventSize,
"vaultURL": r.VaultURL,
Expand All @@ -165,74 +164,74 @@ func (r *PlatformReconciler) reconcile(ctx context.Context, req ctrl.Request, lo
// Ensure there are valid commits for Platform components.
if !api.RegexpCommit.MatchString(build.Info.BrokerCommit) ||
!api.RegexpCommit.MatchString(build.Info.HTTPSrvCommit) {
return p, false, log.ErrorN("broker or httpsrv commit from build info is invalid")
return platform, false, log.ErrorN("broker or httpsrv commit from build info is invalid")
}
if err := r.setupVault(ctx, baseTD); err != nil {
return p, false, log.ErrorN("problem setting up vault: %w", err)
return platform, false, log.ErrorN("problem setting up vault: %w", err)
}
if err := r.ApplyTemplate(ctx, "platform", &baseTD.Data, log); err != nil {
return p, false, log.ErrorN("problem setting up Platform: %w", err)
return platform, false, log.ErrorN("problem setting up Platform: %w", err)
}

r.setSetup(pKey, true)
}

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

td := baseTD.ForComponent(api.PlatformComponentNATS, &appsv1.StatefulSet{}, &NATSDefaults, templates.Component{
Name: api.PlatformComponentNATS,
Image: NATSImage,
PodSpec: p.Spec.NATS.PodSpec,
ContainerSpec: p.Spec.NATS.ContainerSpec,
PodSpec: platform.Spec.NATS.PodSpec,
ContainerSpec: platform.Spec.NATS.ContainerSpec,
IsPlatformComponent: true,
})
if err := r.setupVaultComponent(ctx, td, ""); err != nil {
return p, false, err
return platform, false, err
}
if rdy, err := r.CompMgr.SetupComponent(ctx, td); !rdy || err != nil {
chill()
return p, rdy, err
return platform, rdy, err
}

td = baseTD.ForComponent(api.PlatformComponentBroker, &appsv1.DaemonSet{}, &BrokerDefaults, templates.Component{
Name: api.PlatformComponentBroker,
Commit: build.Info.BrokerCommit,
Image: BrokerImage,
PodSpec: p.Spec.Broker.PodSpec,
ContainerSpec: p.Spec.Broker.ContainerSpec,
PodSpec: platform.Spec.Broker.PodSpec,
ContainerSpec: platform.Spec.Broker.ContainerSpec,
IsPlatformComponent: true,
})
if err := r.setupVaultComponent(ctx, td, ""); err != nil {
return p, false, err
return platform, false, err
}
if rdy, err := r.CompMgr.SetupComponent(ctx, td); !rdy || err != nil {
chill()
return p, rdy, err
return platform, rdy, err
}

td = baseTD.ForComponent(api.PlatformComponentHTTPSrv, &appsv1.Deployment{}, &HTTPSrvDefaults, templates.Component{
Name: api.PlatformComponentHTTPSrv,
Commit: build.Info.HTTPSrvCommit,
Image: HTTPSrvImage,
PodSpec: p.Spec.HTTPSrv.PodSpec,
ContainerSpec: p.Spec.HTTPSrv.ContainerSpec,
PodSpec: platform.Spec.HTTPSrv.PodSpec,
ContainerSpec: platform.Spec.HTTPSrv.ContainerSpec,
IsPlatformComponent: true,
})
td.Values["serviceType"] = p.Spec.HTTPSrv.Service.Type
td.Values["httpPort"] = p.Spec.HTTPSrv.Service.Ports.HTTP
td.Values["httpsPort"] = p.Spec.HTTPSrv.Service.Ports.HTTPS
td.Values["serviceType"] = platform.Spec.HTTPSrv.Service.Type
td.Values["httpPort"] = platform.Spec.HTTPSrv.Service.Ports.HTTP
td.Values["httpsPort"] = platform.Spec.HTTPSrv.Service.Ports.HTTPS
if err := r.setupVaultComponent(ctx, td, ""); err != nil {
return p, false, err
return platform, false, err
}
if rdy, err := r.CompMgr.SetupComponent(ctx, td); !rdy || err != nil {
chill()
return p, rdy, err
return platform, rdy, err
}

return p, true, nil
return platform, true, nil
}

func (r *PlatformReconciler) updateComponentsStatus(ctx context.Context, p *v1alpha1.Platform) error {
Expand Down Expand Up @@ -426,23 +425,23 @@ func (r *PlatformReconciler) vaultClient(ctx context.Context, url string, caCert
}

func (r *PlatformReconciler) setDefaults(platform *v1alpha1.Platform) bool {
s := &platform.Spec
cur := s.DeepCopy()
spec := &platform.Spec
origSpec := spec.DeepCopy()

if s.Logger.Format == "" {
s.Logger.Format = api.DefaultLogFormat
if spec.Logger.Format == "" {
spec.Logger.Format = api.DefaultLogFormat
}
if s.Logger.Level == "" {
s.Logger.Level = api.DefaultLogLevel
if spec.Logger.Level == "" {
spec.Logger.Level = api.DefaultLogLevel
}
if s.Events.TimeoutSeconds == 0 {
s.Events.TimeoutSeconds = api.DefaultTimeoutSeconds
if spec.Events.TimeoutSeconds == 0 {
spec.Events.TimeoutSeconds = api.DefaultTimeoutSeconds
}
if s.Events.MaxSize.IsZero() {
s.Events.MaxSize.Set(api.DefaultMaxEventSizeBytes)
if spec.Events.MaxSize.IsZero() {
spec.Events.MaxSize.Set(api.DefaultMaxEventSizeBytes)
}

svc := &s.HTTPSrv.Service
svc := &spec.HTTPSrv.Service
if svc.Type == "" {
svc.Type = "ClusterIP"
}
Expand All @@ -453,11 +452,11 @@ func (r *PlatformReconciler) setDefaults(platform *v1alpha1.Platform) bool {
svc.Ports.HTTPS = 443
}

SetDefaults(&s.NATS.ContainerSpec, &NATSDefaults)
SetDefaults(&s.Broker.ContainerSpec, &BrokerDefaults)
SetDefaults(&s.HTTPSrv.ContainerSpec, &HTTPSrvDefaults)
SetDefaults(&spec.NATS.ContainerSpec, &NATSDefaults)
SetDefaults(&spec.Broker.ContainerSpec, &BrokerDefaults)
SetDefaults(&spec.HTTPSrv.ContainerSpec, &HTTPSrvDefaults)

return !k8s.DeepEqual(cur, s)
return !k8s.DeepEqual(spec, origSpec)
}

func (r *PlatformReconciler) setSetup(key string, val bool) {
Expand Down
Loading

0 comments on commit affcd81

Please sign in to comment.