forked from kubevirt/containerized-data-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
123 lines (108 loc) · 4.09 KB
/
factory.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
/*
Copyright 2018 The CDI Authors.
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 operator
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
utils "kubevirt.io/containerized-data-importer/pkg/operator/resources/utils"
)
//FactoryArgs contains the required parameters to generate all cluster-scoped resources
type FactoryArgs struct {
OperatorImage string `required:"true" split_words:"true"`
OperatorVersion string `required:"true" split_words:"true"`
DeployClusterResources string `required:"true" split_words:"true"`
ControllerImage string `required:"true" split_words:"true"`
ImporterImage string `required:"true" split_words:"true"`
ClonerImage string `required:"true" split_words:"true"`
APIServerImage string `required:"true" envconfig:"apiserver_image"`
UploadProxyImage string `required:"true" split_words:"true"`
UploadServerImage string `required:"true" split_words:"true"`
Verbosity string `required:"true"`
PullPolicy string `required:"true" split_words:"true"`
Namespace string
CsvVersion string `required:"true"`
ReplacesCsvVersion string
CDILogo string
}
type operatorFactoryFunc func(*FactoryArgs) []runtime.Object
const (
//OperatorRBAC - operator rbac
OperatorRBAC string = "operator-rbac"
//OperatorDeployment - operator deployment
OperatorDeployment string = "operator-deployment"
//OperatorCdiCRD - operator CRDs
OperatorCdiCRD string = "operator-cdi-crd"
//OperatorConfigMapCR - operartor configmap
OperatorConfigMapCR string = "operator-configmap-cr"
//OperatorCSV - operator csv
OperatorCSV string = "operator-csv"
)
var operatorFactoryFunctions = map[string]operatorFactoryFunc{
OperatorRBAC: createOperatorClusterRBAC,
OperatorDeployment: createOperatorClusterDeployment,
OperatorCdiCRD: createOperatorCDIClusterResource,
OperatorConfigMapCR: createOperatorConfigMapClusterResource,
OperatorCSV: createOperatorClusterServiceVersion,
}
//IsFactoryResource returns true id codeGroupo belolngs to factory functions
func IsFactoryResource(codeGroup string) bool {
for k := range operatorFactoryFunctions {
if codeGroup == k {
return true
}
}
return false
}
//GetOperatorClusterRules returnes operator cluster rules
func GetOperatorClusterRules() *[]rbacv1.PolicyRule {
return getOperatorClusterRules()
}
//GetOperatorDeploymentSpec returns operator deployment spce
func GetOperatorDeploymentSpec(args *FactoryArgs) *appsv1.DeploymentSpec {
return createOperatorDeploymentSpec(args.OperatorVersion,
args.Namespace,
args.DeployClusterResources,
args.OperatorImage,
args.ControllerImage,
args.ImporterImage,
args.ClonerImage,
args.APIServerImage,
args.UploadProxyImage,
args.UploadServerImage,
args.Verbosity,
args.PullPolicy)
}
// CreateAllOperatorResources creates all cluster-wide resources
func CreateAllOperatorResources(args *FactoryArgs) ([]runtime.Object, error) {
var resources []runtime.Object
for group := range operatorFactoryFunctions {
rs, err := CreateOperatorResourceGroup(group, args)
if err != nil {
return nil, err
}
resources = append(resources, rs...)
}
return resources, nil
}
// CreateOperatorResourceGroup creates all cluster resources fr a specific group/component
func CreateOperatorResourceGroup(group string, args *FactoryArgs) ([]runtime.Object, error) {
f, ok := operatorFactoryFunctions[group]
if !ok {
return nil, fmt.Errorf("group %s does not exist", group)
}
resources := f(args)
utils.ValidateGVKs(resources)
return resources, nil
}