-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
136 lines (118 loc) · 3.89 KB
/
project.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
package project
import (
"context"
"github.com/rancher/rancher/pkg/pipeline/remote/model"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
pv3 "github.com/rancher/types/apis/project.cattle.io/v3"
pclient "github.com/rancher/types/client/project/v3"
"github.com/rancher/types/config"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
// This controller is responsible for initializing source code
// provider configs & pipeline settings for projects.
var settings = map[string]string{
utils.SettingExecutorQuota: utils.SettingExecutorQuotaDefault,
utils.SettingSigningDuration: utils.SettingSigningDurationDefault,
}
func Register(ctx context.Context, cluster *config.UserContext) {
projects := cluster.Management.Management.Projects("")
projectSyncer := &Syncer{
configMaps: cluster.Core.ConfigMaps(""),
configMapLister: cluster.Core.ConfigMaps("").Controller().Lister(),
sourceCodeProviderConfigs: cluster.Management.Project.SourceCodeProviderConfigs(""),
pipelineSettings: cluster.Management.Project.PipelineSettings(""),
}
projects.AddClusterScopedHandler("pipeline-controller", cluster.ClusterName, projectSyncer.Sync)
}
type Syncer struct {
configMaps v1.ConfigMapInterface
configMapLister v1.ConfigMapLister
sourceCodeProviderConfigs pv3.SourceCodeProviderConfigInterface
pipelineSettings pv3.PipelineSettingInterface
clusterName string
}
func (l *Syncer) Sync(key string, obj *v3.Project) error {
if obj == nil || obj.DeletionTimestamp != nil {
projectID := ""
splits := strings.Split(key, "/")
if len(splits) == 2 {
projectID = splits[1]
}
return l.cleanInternalRegistryEntry(projectID)
}
if err := l.addSourceCodeProviderConfigs(obj); err != nil {
return err
}
return l.addPipelineSettings(obj)
}
func (l *Syncer) addSourceCodeProviderConfigs(obj *v3.Project) error {
if err := l.addSourceCodeProviderConfig(model.GithubType, pclient.GithubPipelineConfigType, false, obj); err != nil {
return err
}
return l.addSourceCodeProviderConfig(model.GitlabType, pclient.GitlabPipelineConfigType, false, obj)
}
func (l *Syncer) addSourceCodeProviderConfig(name, pType string, enabled bool, obj *v3.Project) error {
_, err := l.sourceCodeProviderConfigs.ObjectClient().Create(&pv3.SourceCodeProviderConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: obj.Name,
},
ProjectName: ref.Ref(obj),
Type: pType,
Enabled: enabled,
})
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func (l *Syncer) addPipelineSettings(obj *v3.Project) error {
for k, v := range settings {
if err := l.addPipelineSetting(k, v, obj); err != nil {
return err
}
}
return nil
}
func (l *Syncer) addPipelineSetting(settingName string, value string, obj *v3.Project) error {
setting := &pv3.PipelineSetting{
ObjectMeta: metav1.ObjectMeta{
Name: settingName,
Namespace: obj.Name,
},
ProjectName: ref.Ref(obj),
Default: value,
}
if _, err := l.pipelineSettings.Create(setting); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func (l *Syncer) cleanInternalRegistryEntry(projectID string) error {
_, projectID = ref.Parse(projectID)
cm, err := l.configMapLister.Get(utils.PipelineNamespace, utils.ProxyConfigMapName)
if apierrors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
portMap, err := utils.GetRegistryPortMapping(cm)
if err != nil {
return err
}
if _, ok := portMap[projectID]; !ok {
return nil
}
delete(portMap, projectID)
toUpdate := cm.DeepCopy()
utils.SetRegistryPortMapping(toUpdate, portMap)
if _, err := l.configMaps.Update(toUpdate); err != nil {
return err
}
return nil
}