Skip to content

Commit

Permalink
feat: Implement kluctl.io/request-validate annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Jul 5, 2023
1 parent a265e9b commit 7b721fd
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
6 changes: 5 additions & 1 deletion api/v1beta1/kluctldeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

KluctlRequestReconcileAnnotation = "kluctl.io/request-reconcile"
KluctlRequestDeployAnnotation = "kluctl.io/request-deploy"
KluctlRequestValidateAnnotation = "kluctl.io/request-validate"
)

type KluctlDeploymentSpec struct {
Expand Down Expand Up @@ -298,7 +299,10 @@ type KluctlDeploymentStatus struct {
LastHandledReconcileAt string `json:"lastHandledReconcileAt,omitempty"`

// +optional
LastHandledDeployAt string `json:"LastHandledDeployAt,omitempty"`
LastHandledDeployAt string `json:"lastHandledDeployAt,omitempty"`

// +optional
LastHandledValidateAt string `json:"lastHandledValidateAt,omitempty"`

// ObservedGeneration is the last reconciled generation.
// +optional
Expand Down
6 changes: 4 additions & 2 deletions config/crd/bases/gitops.kluctl.io_kluctldeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,6 @@ spec:
status:
description: KluctlDeploymentStatus defines the observed state of KluctlDeployment
properties:
LastHandledDeployAt:
type: string
conditions:
items:
description: "Condition contains details for one aspect of the current
Expand Down Expand Up @@ -449,11 +447,15 @@ spec:
description: LastDeployResult is the result of the last deploy command
type: object
x-kubernetes-preserve-unknown-fields: true
lastHandledDeployAt:
type: string
lastHandledReconcileAt:
description: LastHandledReconcileAt holds the value of the most recent
reconcile request value, so a change of the annotation value can
be detected.
type: string
lastHandledValidateAt:
type: string
lastObjectsHash:
type: string
lastValidateError:
Expand Down
6 changes: 4 additions & 2 deletions install/controller/controller/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,6 @@ spec:
status:
description: KluctlDeploymentStatus defines the observed state of KluctlDeployment
properties:
LastHandledDeployAt:
type: string
conditions:
items:
description: "Condition contains details for one aspect of the current
Expand Down Expand Up @@ -449,11 +447,15 @@ spec:
description: LastDeployResult is the result of the last deploy command
type: object
x-kubernetes-preserve-unknown-fields: true
lastHandledDeployAt:
type: string
lastHandledReconcileAt:
description: LastHandledReconcileAt holds the value of the most recent
reconcile request value, so a change of the annotation value can
be detected.
type: string
lastHandledValidateAt:
type: string
lastObjectsHash:
type: string
lastValidateError:
Expand Down
6 changes: 6 additions & 0 deletions pkg/controllers/kluctldeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,12 @@ func (r *KluctlDeploymentReconciler) doReconcile(

oldGeneration := obj.Status.ObservedGeneration
oldDeployRequest := obj.Status.LastHandledDeployAt
oldValidateRequest := obj.Status.LastHandledValidateAt
curDeployRequest, _ := obj.GetAnnotations()[kluctlv1.KluctlRequestDeployAnnotation]
curValidateRequest, _ := obj.GetAnnotations()[kluctlv1.KluctlRequestValidateAnnotation]
obj.Status.ObservedGeneration = obj.GetGeneration()
obj.Status.LastHandledDeployAt = curDeployRequest
obj.Status.LastHandledValidateAt = curValidateRequest

pp, err := prepareProject(ctx, r, obj, true)
if err != nil {
Expand Down Expand Up @@ -278,6 +281,9 @@ func (r *KluctlDeploymentReconciler) doReconcile(
if obj.Status.LastValidateResult == nil || needDeploy {
// either never validated before or a deployment requested (which required re-validation)
needValidate = true
} else if curValidateRequest != "" && oldValidateRequest != curValidateRequest {
// explicitly requested a validate
needValidate = true
} else {
nextValidateTime := r.nextValidateTime(obj)
if nextValidateTime != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func (ReconcileRequestedPredicate) Update(e event.UpdateEvent) bool {
return ok1 != ok2 || v1 != v2
}

return check(kluctlv1.KluctlRequestReconcileAnnotation) || check(kluctlv1.KluctlRequestDeployAnnotation)
return check(kluctlv1.KluctlRequestReconcileAnnotation) || check(kluctlv1.KluctlRequestDeployAnnotation) || check(kluctlv1.KluctlRequestValidateAnnotation)
}
23 changes: 4 additions & 19 deletions pkg/webui/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,25 +372,6 @@ func (s *CommandResultsServer) getValidateResult(c *gin.Context) {
c.JSON(http.StatusOK, vr)
}

func (s *CommandResultsServer) validateNow(c *gin.Context) {
var params ProjectTargetKey
err := c.Bind(&params)
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}

key := ProjectTargetKey{
Project: params.Project,
Target: params.Target,
}

_ = key
// TODO

c.Status(http.StatusOK)
}

type kluctlDeploymentParam struct {
Cluster string `json:"cluster"`
Name string `json:"name"`
Expand Down Expand Up @@ -444,6 +425,10 @@ func (s *CommandResultsServer) doSetAnnotation(c *gin.Context, aname string, ava
c.Status(http.StatusOK)
}

func (s *CommandResultsServer) validateNow(c *gin.Context) {
s.doSetAnnotation(c, kluctlv1.KluctlRequestValidateAnnotation, time.Now().Format(time.RFC3339Nano))
}

func (s *CommandResultsServer) reconcileNow(c *gin.Context) {
s.doSetAnnotation(c, kluctlv1.KluctlRequestReconcileAnnotation, time.Now().Format(time.RFC3339Nano))
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/webui/ui/src/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface Api {
getCommandResultSummary(resultId: string): Promise<CommandResultSummary>
getCommandResultObject(resultId: string, ref: ObjectRef, objectType: string): Promise<any>
getValidateResult(resultId: string): Promise<ValidateResult>
validateNow(project: ProjectKey, target: TargetKey): Promise<Response>
validateNow(cluster: string, name: string, namespace: string): Promise<Response>
reconcileNow(cluster: string, name: string, namespace: string): Promise<Response>
deployNow(cluster: string, name: string, namespace: string): Promise<Response>
}
Expand Down Expand Up @@ -241,10 +241,11 @@ export class RealApi implements Api {
return new ValidateResult(json)
}

async validateNow(project: ProjectKey, target: TargetKey) {
async validateNow(cluster: string, name: string, namespace: string) {
return this.doPost("/api/validateNow", {
"project": project,
"target": target,
"cluster": cluster,
"name": name,
"namespace": namespace,
})
}

Expand Down Expand Up @@ -324,7 +325,7 @@ export class StaticApi implements Api {
throw new Error("not implemented")
}

validateNow(project: ProjectKey, target: TargetKey): Promise<Response> {
validateNow(cluster: string, name: string, namespace: string): Promise<Response> {
throw new Error("not implemented")
}

Expand Down
15 changes: 7 additions & 8 deletions pkg/webui/ui/src/components/targets-view/TargetItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ export const TargetItem = React.memo(React.forwardRef((
const api = useContext(ApiContext)
const actionMenuItems: ActionMenuItem[] = []

actionMenuItems.push({
icon: <PublishedWithChanges />,
text: "Validate now",
handler: () => {
api.validateNow(props.ps.project, props.ts.target)
}
})

let kd: KluctlDeploymentInfo | undefined
let allKdEqual = true
props.ts.commandResults?.forEach(rs => {
Expand All @@ -128,6 +120,13 @@ export const TargetItem = React.memo(React.forwardRef((
})

if (kd && allKdEqual) {
actionMenuItems.push({
icon: <PublishedWithChanges />,
text: "Validate",
handler: () => {
api.validateNow(props.ts.target.clusterId, kd!.name, kd!.namespace)
}
})
actionMenuItems.push({
icon: <PublishedWithChanges />,
text: "Reconcile",
Expand Down

0 comments on commit 7b721fd

Please sign in to comment.