This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
forked from robolaunch/robot-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_webhook.go
150 lines (111 loc) · 4.14 KB
/
manager_webhook.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
/*
Copyright 2022.
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 v1alpha1
import (
"context"
"errors"
"net/url"
"strings"
"github.com/google/go-github/github"
"github.com/robolaunch/devspace-operator/internal"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// ********************************
// WorkspaceManager webhooks
// ********************************
// log is for logging in this package.
var workspacemanagerlog = logf.Log.WithName("workspacemanager-resource")
func (r *WorkspaceManager) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
//+kubebuilder:webhook:path=/mutate-dev-roboscale-io-v1alpha1-workspacemanager,mutating=true,failurePolicy=fail,sideEffects=None,groups=dev.roboscale.io,resources=workspacemanagers,verbs=create;update,versions=v1alpha1,name=mworkspacemanager.kb.io,admissionReviewVersions=v1
var _ webhook.Defaulter = &WorkspaceManager{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *WorkspaceManager) Default() {
workspacemanagerlog.Info("default", "name", r.Name)
// _ = r.setRepositoryInfo()
}
//+kubebuilder:webhook:path=/validate-dev-roboscale-io-v1alpha1-workspacemanager,mutating=false,failurePolicy=fail,sideEffects=None,groups=dev.roboscale.io,resources=workspacemanagers,verbs=create;update,versions=v1alpha1,name=vworkspacemanager.kb.io,admissionReviewVersions=v1
var _ webhook.Validator = &WorkspaceManager{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *WorkspaceManager) ValidateCreate() error {
workspacemanagerlog.Info("validate create", "name", r.Name)
err := r.checkTargetDevSpaceLabel()
if err != nil {
return err
}
return nil
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *WorkspaceManager) ValidateUpdate(old runtime.Object) error {
workspacemanagerlog.Info("validate update", "name", r.Name)
err := r.checkTargetDevSpaceLabel()
if err != nil {
return err
}
return nil
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *WorkspaceManager) ValidateDelete() error {
workspacemanagerlog.Info("validate delete", "name", r.Name)
return nil
}
func (r *WorkspaceManager) checkTargetDevSpaceLabel() error {
labels := r.GetLabels()
if _, ok := labels[internal.TARGET_DEVSPACE_LABEL_KEY]; !ok {
return errors.New("target devspace label should be added with key " + internal.TARGET_DEVSPACE_LABEL_KEY)
}
return nil
}
func (r *WorkspaceManager) setRepositoryInfo() error {
for k1, ws := range r.Spec.Workspaces {
for k2, repo := range ws.Repositories {
userOrOrg, repoName, err := getPathVariables(repo.URL)
if err != nil {
return err
}
repo.Owner = userOrOrg
repo.Repo = repoName
lastCommitHash, err := getLastCommitHash(repo)
repo.Hash = lastCommitHash
ws.Repositories[k2] = repo
}
r.Spec.Workspaces[k1] = ws
}
return nil
}
func getPathVariables(URL string) (string, string, error) {
u, err := url.Parse(URL)
if err != nil {
return "", "", err
}
path := strings.Split(u.Path, "/")[1:]
if len(path) != 2 {
return "", "", errors.New("path is not right")
}
return path[0], path[1], nil
}
func getLastCommitHash(repository Repository) (string, error) {
client := github.NewClient(nil)
branch, _, err := client.Repositories.GetBranch(context.Background(), repository.Owner, repository.Repo, repository.Branch)
if err != nil {
return "", err
}
commitSHA := branch.Commit.SHA
return *commitSHA, nil
}