/
create.go
142 lines (121 loc) · 4.79 KB
/
create.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
/*
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 client
import (
"context"
"fmt"
"strings"
"time"
mf "github.com/manifestival/manifestival"
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
"github.com/tektoncd/operator/pkg/reconciler/shared/hash"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/logging"
)
func (i *InstallerSetClient) create(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform, isType string) ([]v1alpha1.TektonInstallerSet, error) {
logger := logging.FromContext(ctx).With("kind", i.resourceKind, "type", isType)
if isType == InstallerTypeMain {
sets, err := i.makeMainSets(ctx, comp, manifest, filterAndTransform)
if err != nil {
logger.Errorf("installer set creation failed for main type: %v", err)
return sets, err
}
return sets, nil
}
kind := strings.ToLower(strings.TrimPrefix(i.resourceKind, "Tekton"))
isName := fmt.Sprintf("%s-%s-", kind, isType)
iS, err := i.makeInstallerSet(ctx, comp, manifest, filterAndTransform, isName, isType)
if err != nil {
return nil, err
}
iS, err = i.clientSet.Create(ctx, iS, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return []v1alpha1.TektonInstallerSet{*iS}, nil
}
func (i *InstallerSetClient) makeMainSets(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform) ([]v1alpha1.TektonInstallerSet, error) {
staticManifest := manifest.Filter(mf.Not(mf.ByKind("Deployment")), mf.Not(mf.ByKind("Service")))
deploymentManifest := manifest.Filter(mf.Any(mf.ByKind("Deployment"), mf.ByKind("Service")))
kind := strings.ToLower(strings.TrimPrefix(i.resourceKind, "Tekton"))
staticName := fmt.Sprintf("%s-%s-%s-", kind, InstallerTypeMain, InstallerSubTypeStatic)
staticIS, err := i.makeInstallerSet(ctx, comp, &staticManifest, filterAndTransform, staticName, InstallerTypeMain)
if err != nil {
return nil, err
}
staticIS, err = i.clientSet.Create(ctx, staticIS, metav1.CreateOptions{})
if err != nil {
return nil, err
}
if err := i.waitForStatus(ctx, staticIS); err != nil {
return nil, err
}
deployName := fmt.Sprintf("%s-%s-%s-", kind, InstallerTypeMain, InstallerSubTypeDeployment)
deploymentIS, err := i.makeInstallerSet(ctx, comp, &deploymentManifest, filterAndTransform, deployName, InstallerTypeMain)
if err != nil {
return nil, err
}
deploymentIS, err = i.clientSet.Create(ctx, deploymentIS, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return []v1alpha1.TektonInstallerSet{*staticIS, *deploymentIS}, nil
}
func (i *InstallerSetClient) waitForStatus(ctx context.Context, set *v1alpha1.TektonInstallerSet) error {
for cnt := 0; cnt < 3; cnt++ {
onClusterSet, err := i.clientSet.Get(ctx, set.GetName(), metav1.GetOptions{})
if err != nil {
return err
}
// once status is initialised for static set we can create deployment set
ready := onClusterSet.Status.GetCondition(apis.ConditionReady)
if ready != nil {
return nil
}
// if status is not initialised then wait
time.Sleep(3 * time.Second)
}
// if still the status is not initialised then create the next set and let it fail
// there may be something else wrong
return nil
}
func (i *InstallerSetClient) makeInstallerSet(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform, isName, isType string) (*v1alpha1.TektonInstallerSet, error) {
specHash, err := hash.Compute(comp.GetSpec())
if err != nil {
return nil, err
}
transformedMf, err := filterAndTransform(ctx, manifest, comp)
if err != nil {
return nil, err
}
ownerRef := *metav1.NewControllerRef(comp, v1alpha1.SchemeGroupVersion.WithKind(i.resourceKind))
return &v1alpha1.TektonInstallerSet{
ObjectMeta: metav1.ObjectMeta{
GenerateName: isName,
Labels: map[string]string{
v1alpha1.CreatedByKey: i.resourceKind,
v1alpha1.ReleaseVersionKey: i.releaseVersion,
v1alpha1.InstallerSetType: isType,
},
Annotations: map[string]string{
v1alpha1.TargetNamespaceKey: comp.GetSpec().GetTargetNamespace(),
v1alpha1.LastAppliedHashKey: specHash,
},
OwnerReferences: []metav1.OwnerReference{ownerRef},
},
Spec: v1alpha1.TektonInstallerSetSpec{
Manifests: transformedMf.Resources(),
},
}, nil
}