/
pool.go
82 lines (72 loc) · 2.85 KB
/
pool.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
/*
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"
apiscsp "github.com/openebs/maya/pkg/cstor/poolinstance/v1alpha3"
deploy "github.com/openebs/maya/pkg/kubernetes/deployment/appsv1/v1alpha1"
"github.com/pkg/errors"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
// GetPendingPoolCount returns the pending pool count that should be created for the
// given CStorPoolCluster.
func (c *Config) GetPendingPoolCount() (int, error) {
currentPoolCount, err := c.GetCurrentPoolCount()
if err != nil {
return 0, errors.Wrapf(err, "unable to get pending pool count for cspc %s", c.CSPC.Name)
}
desiredPoolCount := len(c.CSPC.Spec.Pools)
return (desiredPoolCount - currentPoolCount), nil
}
// GetCurrentPoolCount give the current pool count for the given CStorPoolCluster.
func (c *Config) GetCurrentPoolCount() (int, error) {
cspList, err := apiscsp.NewKubeClient().WithNamespace(c.Namespace).List(metav1.ListOptions{LabelSelector: string(apis.CStorPoolClusterCPK) + "=" + c.CSPC.Name})
if err != nil {
return 0, errors.Errorf("unable to get current pool count:unable to list cstor pools: %v", err)
}
return len(cspList.Items), nil
}
// IsPoolPending returns true if pool is pending for creation.
func (c *Config) IsPoolPending() bool {
pc, err := c.GetPendingPoolCount()
if err != nil {
klog.Errorf("unable to get pending pool count : %v", err)
return false
}
if pc > 0 {
return true
}
return false
}
// GetCSPIWithoutDeployment gets the CSPIs for whom the pool deployment does not exists.
func (c *Config) GetCSPIWithoutDeployment() ([]apis.CStorPoolInstance, error) {
var cspList []apis.CStorPoolInstance
cspGotList, err := apiscsp.NewKubeClient().WithNamespace(c.Namespace).List(metav1.ListOptions{LabelSelector: string(apis.CStorPoolClusterCPK) + "=" + c.CSPC.Name})
if err != nil {
return nil, errors.Wrapf(err, "could not list csp for cspc {%s}", c.CSPC.Name)
}
for _, cspObj := range cspGotList.Items {
cspObj := cspObj
_, err := deploy.NewKubeClient().WithNamespace(c.Namespace).Get(cspObj.Name)
if k8serror.IsNotFound(err) {
cspList = append(cspList, cspObj)
continue
}
if err != nil {
klog.Errorf("Could not get pool deployment for csp {%s}", cspObj.Name)
}
}
return cspList, nil
}