-
Notifications
You must be signed in to change notification settings - Fork 186
/
pipelinesascode.go
155 lines (133 loc) · 5.17 KB
/
pipelinesascode.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
/*
Copyright 2022 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 extension
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
op "github.com/tektoncd/operator/pkg/client/clientset/versioned/typed/operator/v1alpha1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
func EnsureOpenShiftPipelinesAsCodeExists(ctx context.Context, clients op.OpenShiftPipelinesAsCodeInterface, config *v1alpha1.TektonConfig) (*v1alpha1.OpenShiftPipelinesAsCode, error) {
opacCR, err := GetPAC(ctx, clients, v1alpha1.OpenShiftPipelinesAsCodeName)
if err != nil {
if !apierrs.IsNotFound(err) {
return nil, err
}
if _, err = createOPAC(ctx, clients, config); err != nil {
return nil, err
}
return nil, v1alpha1.RECONCILE_AGAIN_ERR
}
opacCR, err = updateOPAC(ctx, opacCR, config, clients)
if err != nil {
return nil, err
}
ok, err := isOPACReady(opacCR, err)
if err != nil {
return nil, err
}
if !ok {
return nil, v1alpha1.RECONCILE_AGAIN_ERR
}
return opacCR, err
}
func createOPAC(ctx context.Context, clients op.OpenShiftPipelinesAsCodeInterface, config *v1alpha1.TektonConfig) (*v1alpha1.OpenShiftPipelinesAsCode, error) {
ownerRef := *metav1.NewControllerRef(config, config.GroupVersionKind())
opacCR := &v1alpha1.OpenShiftPipelinesAsCode{
ObjectMeta: metav1.ObjectMeta{
Name: v1alpha1.OpenShiftPipelinesAsCodeName,
OwnerReferences: []metav1.OwnerReference{ownerRef},
},
Spec: v1alpha1.OpenShiftPipelinesAsCodeSpec{
CommonSpec: v1alpha1.CommonSpec{
TargetNamespace: config.Spec.TargetNamespace,
},
Config: config.Spec.Config,
PACSettings: v1alpha1.PACSettings{
Settings: config.Spec.Platforms.OpenShift.PipelinesAsCode.Settings,
},
},
}
if _, err := clients.Create(ctx, opacCR, metav1.CreateOptions{}); err != nil {
return nil, err
}
return opacCR, nil
}
func GetPAC(ctx context.Context, clients op.OpenShiftPipelinesAsCodeInterface, name string) (*v1alpha1.OpenShiftPipelinesAsCode, error) {
return clients.Get(ctx, name, metav1.GetOptions{})
}
func updateOPAC(ctx context.Context, opacCR *v1alpha1.OpenShiftPipelinesAsCode, config *v1alpha1.TektonConfig,
clients op.OpenShiftPipelinesAsCodeInterface) (*v1alpha1.OpenShiftPipelinesAsCode, error) {
// if the pac spec is changed then update the instance
updated := false
if config.Spec.TargetNamespace != opacCR.Spec.TargetNamespace {
opacCR.Spec.TargetNamespace = config.Spec.TargetNamespace
updated = true
}
if !reflect.DeepEqual(opacCR.Spec.Config, config.Spec.Config) {
opacCR.Spec.Config = config.Spec.Config
updated = true
}
if !reflect.DeepEqual(opacCR.Spec.Settings, config.Spec.Platforms.OpenShift.PipelinesAsCode.Settings) {
opacCR.Spec.PACSettings.Settings = config.Spec.Platforms.OpenShift.PipelinesAsCode.PACSettings.Settings
updated = true
}
if opacCR.ObjectMeta.OwnerReferences == nil {
ownerRef := *metav1.NewControllerRef(config, config.GroupVersionKind())
opacCR.ObjectMeta.OwnerReferences = []metav1.OwnerReference{ownerRef}
updated = true
}
if updated {
_, err := clients.Update(ctx, opacCR, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return nil, v1alpha1.RECONCILE_AGAIN_ERR
}
return opacCR, nil
}
// isOPACReady will check the status conditions of the OpenShiftPipelinesAsCode and return true if the OpenShiftPipelinesAsCode is ready.
func isOPACReady(s *v1alpha1.OpenShiftPipelinesAsCode, err error) (bool, error) {
if s.GetStatus() != nil && s.GetStatus().GetCondition(apis.ConditionReady) != nil {
if strings.Contains(s.GetStatus().GetCondition(apis.ConditionReady).Message, v1alpha1.UpgradePending) {
return false, v1alpha1.DEPENDENCY_UPGRADE_PENDING_ERR
}
}
return s.Status.IsReady(), err
}
func EnsureOpenShiftPipelinesAsCodeCRNotExists(ctx context.Context, clients op.OpenShiftPipelinesAsCodeInterface) error {
if _, err := GetPAC(ctx, clients, v1alpha1.OpenShiftPipelinesAsCodeName); err != nil {
if apierrs.IsNotFound(err) {
// OpenShiftPipelinesAsCode CR is gone, hence return nil
return nil
}
return err
}
// if the Get was successful, try deleting the CR
if err := clients.Delete(ctx, v1alpha1.OpenShiftPipelinesAsCodeName, metav1.DeleteOptions{}); err != nil {
if apierrs.IsNotFound(err) {
// OpenShiftPipelinesAsCode CR is gone, hence return nil
return nil
}
return fmt.Errorf("OpenShiftPipelinesAsCode %q failed to delete: %v", v1alpha1.OpenShiftPipelinesAsCodeName, err)
}
// if the Delete API call was success,
// then return requeue_event
// so that in a subsequent reconcile call the absence of the CR is verified by one of the 2 checks above
return v1alpha1.RECONCILE_AGAIN_ERR
}