/
team_types.go
147 lines (115 loc) · 3.7 KB
/
team_types.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
/*
Copyright 2019 Amit Kumar Gupta.
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 v1
import (
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/amitkgupta/boshv3/remote-clients"
)
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
// TeamSpec defines the desired state of Team
type TeamSpec struct {
Director string `json:"director"`
}
// TeamStatus defines the observed state of Team
type TeamStatus struct {
Warning string `json:"warning"`
OriginalDirector string `json:"original_director"`
SecretNamespace string `json:"secret_namespace"`
Available bool `json:"available"`
}
// +kubebuilder:object:root=true
// Team is the Schema for the teams API
type Team struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec TeamSpec `json:"spec,omitempty"`
Status TeamStatus `json:"status,omitempty"`
}
func (t Team) BeingDeleted() bool {
return !t.GetDeletionTimestamp().IsZero()
}
var teamFinalizer = strings.Join([]string{"team", finalizerBase}, ".")
func (t Team) hasFinalizer() bool {
return containsString(t.GetFinalizers(), teamFinalizer)
}
func (t *Team) EnsureFinalizer() bool {
changed := !t.hasFinalizer()
t.SetFinalizers(append(t.GetFinalizers(), teamFinalizer))
return changed
}
func (t *Team) EnsureNoFinalizer() bool {
changed := t.hasFinalizer()
t.SetFinalizers(removeString(t.GetFinalizers(), teamFinalizer))
return changed
}
func (t *Team) PrepareToSave(secretNamespace string) (needsStatusUpdate bool) {
originalDirector := t.Status.OriginalDirector
if originalDirector == "" || t.Status.SecretNamespace == "" {
t.Status.OriginalDirector = t.Spec.Director
t.Status.SecretNamespace = secretNamespace
needsStatusUpdate = true
} else {
if t.Spec.Director != originalDirector && t.Status.Warning == "" {
t.Status.Warning = resourceMutationWarning
needsStatusUpdate = true
} else if t.Spec.Director == originalDirector && t.Status.Warning != "" {
t.Status.Warning = ""
needsStatusUpdate = true
}
}
return
}
func (t Team) ClientName() string {
return standardName(t.GetName(), t.GetNamespace())
}
func (t Team) SecretName() string {
return standardName(t.GetName(), t.GetNamespace())
}
func (t Team) SecretNamespace() string {
return t.Status.SecretNamespace
}
func (t *Team) CreateUnlessExists(uc remoteclients.UAAClient, secretData string) error {
if present, err := uc.HasClient(t.ClientName()); err != nil {
return err
} else if !present {
if err := uc.CreateClient(
t.ClientName(),
secretData,
[]string{"bosh.admin"},
); err != nil {
return err
}
}
t.Status.Available = true
return nil
}
func (t Team) DeleteIfExists(uc remoteclients.UAAClient) error {
if present, err := uc.HasClient(t.ClientName()); err != nil {
return err
} else if present {
return uc.DeleteClient(t.ClientName())
}
return nil
}
// +kubebuilder:object:root=true
// TeamList contains a list of Team
type TeamList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Team `json:"items"`
}
func init() {
SchemeBuilder.Register(&Team{}, &TeamList{})
}