/
deployment_cf.go
106 lines (90 loc) · 3.18 KB
/
deployment_cf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package apicurioregistry
import (
"context"
ar "github.com/apicurio/apicurio-operators/apicurio-registry/pkg/apis/apicur/v1alpha1"
apps "k8s.io/api/apps/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var _ ControlFunction = &DeploymentCF{}
type DeploymentCF struct {
ctx *Context
}
func NewDeploymentCF(ctx *Context) ControlFunction {
err := ctx.c.Watch(&source.Kind{Type: &apps.Deployment{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &ar.ApicurioRegistry{},
})
if err != nil {
panic("Error creating Deployment watch.")
}
return &DeploymentCF{ctx: ctx}
}
func (this *DeploymentCF) Describe() string {
return "Deployment Creation"
}
func (this *DeploymentCF) Sense(spec *ar.ApicurioRegistry, request reconcile.Request) error {
// Try to check if there is an existing deployment resource
deploymentName := this.ctx.configuration.GetConfig(CFG_STA_DEPLOYMENT_NAME)
deployments, err := this.ctx.kubecl.client.AppsV1().Deployments(this.ctx.configuration.GetSpecNamespace()).List(
meta.ListOptions{
LabelSelector: "app=" + this.ctx.configuration.GetSpecName(),
})
if err != nil {
return err
}
count := 0
var lastDeployment *apps.Deployment = nil
for _, deployment := range deployments.Items {
if deployment.GetObjectMeta().GetDeletionTimestamp() == nil {
count++
lastDeployment = &deployment
}
}
if deploymentName == "" && count == 0 {
// OK -> No dep. yet
return nil
}
if deploymentName != "" && count == 1 && lastDeployment != nil && deploymentName == lastDeployment.Name {
// OK -> dep exists
return nil
}
if deploymentName == "" && count == 1 && lastDeployment != nil {
// Also OK, but should not happen
// save to status
this.ctx.configuration.SetConfig(CFG_STA_DEPLOYMENT_NAME, lastDeployment.Name)
return nil
}
// bad bad bad!
this.ctx.log.Info("Warning: Inconsistent Deployment state found.")
this.ctx.configuration.ClearConfig(CFG_STA_DEPLOYMENT_NAME)
for _, deployment := range deployments.Items {
// nuke them...
this.ctx.log.Info("Warning: Deleting Deployment '" + deployment.Name + "'.")
_ = this.ctx.kubecl.client.AppsV1().
Deployments(this.ctx.configuration.GetSpecNamespace()).
Delete(deployment.Name, &meta.DeleteOptions{})
}
return nil
}
func (this *DeploymentCF) Compare(spec *ar.ApicurioRegistry) (bool, error) {
return this.ctx.configuration.GetConfig(CFG_STA_DEPLOYMENT_NAME) == "", nil
}
func (this *DeploymentCF) Respond(spec *ar.ApicurioRegistry) (bool, error) {
deployment := this.ctx.factory.CreateDeployment()
if err := controllerutil.SetControllerReference(spec, deployment, this.ctx.scheme); err != nil {
log.Error(err, "Cannot set controller reference.")
return true, err
}
if err := this.ctx.client.Create(context.TODO(), deployment); err != nil {
log.Error(err, "Failed to create a new Deployment.")
return true, err
} else {
this.ctx.configuration.SetConfig(CFG_STA_DEPLOYMENT_NAME, deployment.Name)
log.Info("New Deployment name is " + deployment.Name)
}
return true, nil
}