-
Notifications
You must be signed in to change notification settings - Fork 9
/
cfssl_ca_secret.go
140 lines (125 loc) · 4.28 KB
/
cfssl_ca_secret.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
This file is part of Astarte.
Copyright 2020 Ispirata Srl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// TODO: Deprecate and kill this file when we are no longer supporting < 1.0
package reconcile
import (
"context"
"github.com/openlyinc/pointy"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
apiv1alpha1 "github.com/astarte-platform/astarte-kubernetes-operator/apis/api/v1alpha1"
)
// EnsureCFSSLCASecret reconciles CFSSL's CA Secret
func EnsureCFSSLCASecret(cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
jobName := cr.Name + "-cfssl-ca-secret-job"
secretName := cr.Name + "-cfssl-ca"
serviceAccountName := jobName
// First of all, ensure we have the right roles.
if pointy.BoolValue(cr.Spec.RBAC, true) {
if err := reconcileStandardRBACForClusteringForApp(jobName, getCFSSLCAJobPolicyRules(), cr, c, scheme); err != nil {
return err
}
} else {
serviceAccountName = ""
}
// Now - is the secret there?
secretThere := false
theSecret := &v1.Secret{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: secretName, Namespace: cr.Namespace}, theSecret); err == nil {
// The secret is on.
secretThere = true
}
// Is the Job up and running?
jobThere := false
theJob := &batchv1.Job{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: jobName, Namespace: cr.Namespace}, theJob); err == nil {
// The Job is on.
jobThere = true
}
// Let's see what to do.
reqLogger := log.WithValues("Request.Namespace", cr.Namespace, "Request.Name", cr.Name)
switch {
case secretThere && jobThere:
// Delete the Job.
reqLogger.Info("Deleting stale CFSSL CA Job")
return c.Delete(context.TODO(), theJob)
case !secretThere && !jobThere:
// Create the Job
reqLogger.Info("Creating CFSSL CA Job")
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{Name: jobName, Namespace: cr.Namespace},
Spec: batchv1.JobSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Name: jobName},
Spec: v1.PodSpec{
ServiceAccountName: serviceAccountName,
ImagePullSecrets: cr.Spec.ImagePullSecrets,
RestartPolicy: v1.RestartPolicyNever,
Containers: []v1.Container{{
Name: jobName,
Image: getAstarteImageFromChannel("cfssl-kubernetes-secret", "latest", cr),
ImagePullPolicy: getImagePullPolicy(cr),
Env: []v1.EnvVar{
{
Name: "CFSSL_URL",
Value: getCFSSLURL(cr),
},
{
Name: "SECRET_NAME",
Value: secretName,
},
},
}},
},
},
},
}
if err := controllerutil.SetControllerReference(cr, job, scheme); err != nil {
return err
}
return c.Create(context.TODO(), job)
case !secretThere && jobThere:
// If the job failed, take action. Otherwise, just skip this.
for _, condition := range theJob.Status.Conditions {
if condition.Type == batchv1.JobFailed && condition.Status == v1.ConditionTrue {
// The Job has failed, but no secret is available. Let's delete the job and wait
// for the next reconciliation.
reqLogger.Info("CFSSL CA Job failed. Deleting it, and waiting for next reconciliation to recreate it")
return c.Delete(context.TODO(), theJob)
}
}
}
// We should not be here ever. But just in case.
return nil
}
func getCFSSLCAJobPolicyRules() []rbacv1.PolicyRule {
return []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"secrets"},
Verbs: []string{"create"},
},
{
APIGroups: []string{""},
Resources: []string{"pods"},
Verbs: []string{"list"},
},
}
}