/
build_csp.go
89 lines (77 loc) · 2.95 KB
/
build_csp.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
/*
Copyright 2019 The OpenEBS 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 v1alpha2
import (
apis "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
apicspsc "github.com/openebs/maya/pkg/cstor/poolcluster/v1alpha1"
apiscsp "github.com/openebs/maya/pkg/cstor/poolinstance/v1alpha3"
"github.com/openebs/maya/pkg/version"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/rand"
)
const (
// CasTypeCStor is the key for cas type cStor
CasTypeCStor = "cstor"
)
// GetCSPSpec returns a CSPI spec that should be created and claims all the
// block device present in the CSPI spec
func (ac *Config) GetCSPSpec() (*apis.CStorPoolInstance, error) {
poolSpec, nodeName, err := ac.SelectNode()
if err != nil || nodeName == "" {
return nil, errors.Wrap(err, "failed to select a node")
}
if poolSpec.PoolConfig.Resources == nil {
poolSpec.PoolConfig.Resources = ac.CSPC.Spec.DefaultResources
}
if poolSpec.PoolConfig.AuxResources == nil {
poolSpec.PoolConfig.AuxResources = ac.CSPC.Spec.DefaultAuxResources
}
if len(poolSpec.PoolConfig.Tolerations) == 0 {
poolSpec.PoolConfig.Tolerations = ac.CSPC.Spec.Tolerations
}
if poolSpec.PoolConfig.PriorityClassName == "" {
poolSpec.PoolConfig.PriorityClassName = ac.CSPC.Spec.DefaultPriorityClassName
}
csplabels := ac.buildLabelsForCSPI(nodeName)
cspObj, err := apiscsp.NewBuilder().
WithName(ac.CSPC.Name + "-" + rand.String(4)).
WithNamespace(ac.Namespace).
WithNodeSelectorByReference(poolSpec.NodeSelector).
WithNodeName(nodeName).
WithPoolConfig(&poolSpec.PoolConfig).
WithRaidGroups(poolSpec.RaidGroups).
WithCSPCOwnerReference(ac.CSPC).
WithLabelsNew(csplabels).
WithFinalizer(apicspsc.CSPCFinalizer).
WithNewVersion(version.GetVersion()).
WithDependentsUpgraded().
Build()
if err != nil {
return nil, errors.Wrapf(err, "failed to build CSPI object for node selector {%v}", poolSpec.NodeSelector)
}
err = ac.ClaimBDsForNode(ac.GetBDListForNode(poolSpec))
if err != nil {
return nil, errors.Wrapf(err, "failed to claim block devices for node {%s}", nodeName)
}
return cspObj.Object, nil
}
// buildLabelsForCSPI builds labels for CSPI
// TODO : Improve following using builders
func (ac *Config) buildLabelsForCSPI(nodeName string) map[string]string {
labels := make(map[string]string)
labels[HostName] = nodeName
labels[string(apis.CStorPoolClusterCPK)] = ac.CSPC.Name
labels[string(apis.OpenEBSVersionKey)] = version.GetVersion()
labels[string(apis.CASTypeKey)] = CasTypeCStor
return labels
}