-
Notifications
You must be signed in to change notification settings - Fork 185
/
tektoninstallerset.go
184 lines (150 loc) · 5.82 KB
/
tektoninstallerset.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Copyright 2021 The Tekton Authors
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.
*/
package tektoninstallerset
import (
"context"
"fmt"
mf "github.com/manifestival/manifestival"
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
clientset "github.com/tektoncd/operator/pkg/client/clientset/versioned"
tektonInstallerreconciler "github.com/tektoncd/operator/pkg/client/injection/reconciler/operator/v1alpha1/tektoninstallerset"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
)
// Reconciler implements controller.Reconciler for TektonInstallerSet resources.
type Reconciler struct {
operatorClientSet clientset.Interface
mfClient mf.Client
}
// Reconciler implements controller.Reconciler
var _ tektonInstallerreconciler.Interface = (*Reconciler)(nil)
var _ tektonInstallerreconciler.Finalizer = (*Reconciler)(nil)
// FinalizeKind removes all resources after deletion of a TektonInstallerSet.
func (r *Reconciler) FinalizeKind(ctx context.Context, installerSet *v1alpha1.TektonInstallerSet) pkgreconciler.Event {
logger := logging.FromContext(ctx)
deleteManifests, err := mf.ManifestFrom(installerSet.Spec.Manifests, mf.UseClient(r.mfClient))
if err != nil {
logger.Error("Error creating initial manifest: ", err)
installerSet.Status.MarkNotReady(fmt.Sprintf("Internal Error: failed to create manifest: %s", err.Error()))
return err
}
installer := NewInstaller(&deleteManifests, r.mfClient, logger)
err = installer.DeleteResources()
if err != nil {
logger.Error("failed to delete resources: ", err)
return err
}
return nil
}
// Returns ownerReference to add in resource while installing
func getReference(tis *v1alpha1.TektonInstallerSet) []v1.OwnerReference {
return []v1.OwnerReference{*v1.NewControllerRef(tis, tis.GetGroupVersionKind())}
}
// ReconcileKind compares the actual state with the desired, and attempts to
// converge the two.
func (r *Reconciler) ReconcileKind(ctx context.Context, installerSet *v1alpha1.TektonInstallerSet) pkgreconciler.Event {
installerSet.Status.InitializeConditions()
logger := logging.FromContext(ctx).With("installerSet", fmt.Sprintf("%s/%s", installerSet.Namespace, installerSet.Name))
installManifests, err := mf.ManifestFrom(installerSet.Spec.Manifests, mf.UseClient(r.mfClient))
if err != nil {
logger.Error("Error creating initial manifest: ", err)
installerSet.Status.MarkNotReady(fmt.Sprintf("Internal Error: failed to create manifest: %s", err.Error()))
return err
}
// Set owner of InstallerSet as owner of CRDs so that
// deleting the installer will not delete the CRDs and Namespace
// If installerSet has not set any owner then CRDs will
// not have any owner
installerSetOwner := installerSet.GetOwnerReferences()
installManifests, err = installManifests.Transform(
injectOwner(getReference(installerSet)),
injectOwnerForCRDsAndNamespace(installerSetOwner),
)
if err != nil {
logger.Error("failed to transform manifest")
return err
}
installer := NewInstaller(&installManifests, r.mfClient, logger)
// Install CRDs
err = installer.EnsureCRDs()
if err != nil {
installerSet.Status.MarkCRDsInstallationFailed(err.Error())
return r.handleError(err, installerSet)
}
// Update Status for CRD condition
installerSet.Status.MarkCRDsInstalled()
// Install ClusterScoped Resources
err = installer.EnsureClusterScopedResources()
if err != nil {
installerSet.Status.MarkClustersScopedInstallationFailed(err.Error())
return r.handleError(err, installerSet)
}
// Update Status for ClustersScope Condition
installerSet.Status.MarkClustersScopedResourcesInstalled()
// Install NamespaceScoped Resources
err = installer.EnsureNamespaceScopedResources()
if err != nil {
installerSet.Status.MarkNamespaceScopedInstallationFailed(err.Error())
return r.handleError(err, installerSet)
}
// Update Status for NamespaceScope Condition
installerSet.Status.MarkNamespaceScopedResourcesInstalled()
// Install Deployment Resources
err = installer.EnsureDeploymentResources()
if err != nil {
installerSet.Status.MarkDeploymentsAvailableFailed(err.Error())
return r.handleError(err, installerSet)
}
// Update Status for Deployment Resources
installerSet.Status.MarkDeploymentsAvailable()
// Check if webhook is ready
err = installer.IsWebhookReady()
if err != nil {
installerSet.Status.MarkWebhookNotReady(err.Error())
return nil
}
// Update Status for Webhook
installerSet.Status.MarkWebhookReady()
// Check if controller is ready
err = installer.IsControllerReady()
if err != nil {
installerSet.Status.MarkControllerNotReady(err.Error())
return nil
}
// Update Ready status of Controller
installerSet.Status.MarkControllerReady()
// job
labels := installerSet.GetLabels()
installSetname := installerSet.GetName()
err = installer.IsJobCompleted(ctx, labels, installSetname)
if err != nil {
return err
}
// Check if any other deployment exists other than controller
// and webhook and is ready
err = installer.AllDeploymentsReady()
if err != nil {
installerSet.Status.MarkAllDeploymentsNotReady(err.Error())
return nil
}
// Mark all deployments ready
installerSet.Status.MarkAllDeploymentsReady()
return nil
}
func (r *Reconciler) handleError(err error, installerSet *v1alpha1.TektonInstallerSet) error {
if err == v1alpha1.RECONCILE_AGAIN_ERR {
return v1alpha1.REQUEUE_EVENT_AFTER
}
return err
}