forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
158 lines (138 loc) · 5.19 KB
/
util.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
156
157
158
package util
import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
internalclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
oapi "github.com/openshift/origin/pkg/api"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
projectapiv1 "github.com/openshift/origin/pkg/project/apis/project/v1"
)
// Associated returns true if the spec.finalizers contains the origin finalizer
func Associated(namespace *kapi.Namespace) bool {
for i := range namespace.Spec.Finalizers {
if projectapi.FinalizerOrigin == namespace.Spec.Finalizers[i] {
return true
}
}
return false
}
// Associate adds the origin finalizer to spec.finalizers if its not there already
func Associate(kubeClient internalclientset.Interface, namespace *kapi.Namespace) (*kapi.Namespace, error) {
if Associated(namespace) {
return namespace, nil
}
return finalizeInternal(kubeClient, namespace, true)
}
// Finalized returns true if the spec.finalizers does not contain the origin finalizer
func Finalized(namespace *v1.Namespace) bool {
for i := range namespace.Spec.Finalizers {
if projectapiv1.FinalizerOrigin == namespace.Spec.Finalizers[i] {
return false
}
}
return true
}
// Finalize will remove the origin finalizer from the namespace
func Finalize(kubeClient clientset.Interface, namespace *v1.Namespace) (result *v1.Namespace, err error) {
if Finalized(namespace) {
return namespace, nil
}
// there is a potential for a resource conflict with base kubernetes finalizer
// as a result, we handle resource conflicts in case multiple finalizers try
// to finalize at same time
for {
result, err = finalizeInternalV1(kubeClient, namespace, false)
if err == nil {
return result, nil
}
if !kerrors.IsConflict(err) {
return nil, err
}
namespace, err = kubeClient.Core().Namespaces().Get(namespace.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
}
}
// finalizeInternal will update the namespace finalizer list to either have or not have origin finalizer
// TODO: remove me
func finalizeInternal(kubeClient internalclientset.Interface, namespace *kapi.Namespace, withOrigin bool) (*kapi.Namespace, error) {
namespaceFinalize := kapi.Namespace{}
namespaceFinalize.ObjectMeta = namespace.ObjectMeta
namespaceFinalize.Spec = namespace.Spec
finalizerSet := sets.NewString()
for i := range namespace.Spec.Finalizers {
finalizerSet.Insert(string(namespace.Spec.Finalizers[i]))
}
if withOrigin {
finalizerSet.Insert(string(projectapi.FinalizerOrigin))
} else {
finalizerSet.Delete(string(projectapi.FinalizerOrigin))
}
namespaceFinalize.Spec.Finalizers = make([]kapi.FinalizerName, 0, len(finalizerSet))
for _, value := range finalizerSet.List() {
namespaceFinalize.Spec.Finalizers = append(namespaceFinalize.Spec.Finalizers, kapi.FinalizerName(value))
}
return kubeClient.Core().Namespaces().Finalize(&namespaceFinalize)
}
// finalizeInternalV1 will update the namespace finalizer list to either have or not have origin finalizer
func finalizeInternalV1(kubeClient clientset.Interface, namespace *v1.Namespace, withOrigin bool) (*v1.Namespace, error) {
namespaceFinalize := v1.Namespace{}
namespaceFinalize.ObjectMeta = namespace.ObjectMeta
namespaceFinalize.Spec = namespace.Spec
finalizerSet := sets.NewString()
for i := range namespace.Spec.Finalizers {
finalizerSet.Insert(string(namespace.Spec.Finalizers[i]))
}
if withOrigin {
finalizerSet.Insert(string(projectapiv1.FinalizerOrigin))
} else {
finalizerSet.Delete(string(projectapiv1.FinalizerOrigin))
}
namespaceFinalize.Spec.Finalizers = make([]v1.FinalizerName, 0, len(finalizerSet))
for _, value := range finalizerSet.List() {
namespaceFinalize.Spec.Finalizers = append(namespaceFinalize.Spec.Finalizers, v1.FinalizerName(value))
}
return kubeClient.Core().Namespaces().Finalize(&namespaceFinalize)
}
// ConvertNamespace transforms a Namespace into a Project
func ConvertNamespace(namespace *kapi.Namespace) *projectapi.Project {
return &projectapi.Project{
ObjectMeta: namespace.ObjectMeta,
Spec: projectapi.ProjectSpec{
Finalizers: namespace.Spec.Finalizers,
},
Status: projectapi.ProjectStatus{
Phase: namespace.Status.Phase,
},
}
}
// convertProject transforms a Project into a Namespace
func ConvertProject(project *projectapi.Project) *kapi.Namespace {
namespace := &kapi.Namespace{
ObjectMeta: project.ObjectMeta,
Spec: kapi.NamespaceSpec{
Finalizers: project.Spec.Finalizers,
},
Status: kapi.NamespaceStatus{
Phase: project.Status.Phase,
},
}
if namespace.Annotations == nil {
namespace.Annotations = map[string]string{}
}
namespace.Annotations[oapi.OpenShiftDisplayName] = project.Annotations[oapi.OpenShiftDisplayName]
return namespace
}
// ConvertNamespaceList transforms a NamespaceList into a ProjectList
func ConvertNamespaceList(namespaceList *kapi.NamespaceList) *projectapi.ProjectList {
projects := &projectapi.ProjectList{}
for _, n := range namespaceList.Items {
projects.Items = append(projects.Items, *ConvertNamespace(&n))
}
return projects
}