-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
util.go
92 lines (81 loc) · 3.06 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
package util
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/generic"
apistorage "k8s.io/apiserver/pkg/storage"
kapi "k8s.io/kubernetes/pkg/apis/core"
oapi "github.com/openshift/origin/pkg/api"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
)
// ConvertNamespaceFromExternal transforms a versioned Namespace into a Project
func ConvertNamespaceFromExternal(namespace *corev1.Namespace) *projectapi.Project {
internalFinalizers := []kapi.FinalizerName{}
for _, externalFinalizer := range namespace.Spec.Finalizers {
internalFinalizers = append(internalFinalizers, kapi.FinalizerName(externalFinalizer))
}
return &projectapi.Project{
ObjectMeta: namespace.ObjectMeta,
Spec: projectapi.ProjectSpec{
Finalizers: internalFinalizers,
},
Status: projectapi.ProjectStatus{
Phase: kapi.NamespacePhase(namespace.Status.Phase),
},
}
}
func ConvertProjectToExternal(project *projectapi.Project) *corev1.Namespace {
externalFinalizers := []corev1.FinalizerName{}
for _, internalFinalizer := range project.Spec.Finalizers {
externalFinalizers = append(externalFinalizers, corev1.FinalizerName(internalFinalizer))
}
namespace := &corev1.Namespace{
ObjectMeta: project.ObjectMeta,
Spec: corev1.NamespaceSpec{
Finalizers: externalFinalizers,
},
Status: corev1.NamespaceStatus{
Phase: corev1.NamespacePhase(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 *corev1.NamespaceList) *projectapi.ProjectList {
projects := &projectapi.ProjectList{}
for _, n := range namespaceList.Items {
projects.Items = append(projects.Items, *ConvertNamespaceFromExternal(&n))
}
return projects
}
// getAttrs returns labels and fields of a given object for filtering purposes.
func getAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
projectObj, ok := obj.(*projectapi.Project)
if !ok {
return nil, nil, false, fmt.Errorf("not a project")
}
return labels.Set(projectObj.Labels), projectToSelectableFields(projectObj), projectObj.Initializers != nil, nil
}
// MatchProject returns a generic matcher for a given label and field selector.
func MatchProject(label labels.Selector, field fields.Selector) apistorage.SelectionPredicate {
return apistorage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: getAttrs,
}
}
// projectToSelectableFields returns a field set that represents the object
func projectToSelectableFields(projectObj *projectapi.Project) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&projectObj.ObjectMeta, false)
specificFieldsSet := fields.Set{
"status.phase": string(projectObj.Status.Phase),
}
return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
}