/
objectvalidator.go
114 lines (98 loc) · 3.05 KB
/
objectvalidator.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
/*
Copyright 2021 The AtomCI Group 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 kuberes
import (
"fmt"
"github.com/go-atomci/atomci/internal/middleware/log"
v1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type ObjectValidator interface {
Validator(obj interface{}) error
}
type KubeAppValidator struct {
namespace string
client kubernetes.Interface
}
func NewKubeAppValidator(client kubernetes.Interface, namespace string) *KubeAppValidator {
return &KubeAppValidator{
namespace: namespace,
client: client,
}
}
func (validator *KubeAppValidator) Validator(obj interface{}) error {
var volumes []apiv1.Volume
if obj == nil {
return nil
}
if dp, ok := obj.(*v1.Deployment); ok {
volumes = dp.Spec.Template.Spec.Volumes
}
for _, vol := range volumes {
if vol.PersistentVolumeClaim != nil {
_, err := validator.client.CoreV1().PersistentVolumeClaims(validator.namespace).Get(vol.PersistentVolumeClaim.ClaimName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return fmt.Errorf("PVC(%s) is not existed in namespace %s", vol.PersistentVolumeClaim.ClaimName, validator.namespace)
}
if err != nil {
log.Log.Error("get PVC info failed:", err)
return fmt.Errorf("get PVC(%s) info failed", vol.PersistentVolumeClaim.ClaimName)
}
}
if vol.ConfigMap != nil {
_, err := validator.client.CoreV1().ConfigMaps(validator.namespace).Get(vol.ConfigMap.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return fmt.Errorf("Configmap:%s is not existed in namespace %s", vol.ConfigMap.Name, validator.namespace)
}
if err != nil {
log.Log.Error("get configmap info failed:", err)
return fmt.Errorf("get configmap:%s info failed", vol.ConfigMap.Name)
}
}
}
return nil
}
type KubeSvcValidator struct {
cluster string
namespace string
appname string
}
func NewKubeSvcValidator(cluster, namespace, appname string) *KubeSvcValidator {
return &KubeSvcValidator{
cluster: cluster,
namespace: namespace,
appname: appname,
}
}
func (validator *KubeSvcValidator) Validator(obj interface{}) error {
if obj == nil {
return nil
}
svc, ok := obj.(*apiv1.Service)
if !ok {
return nil
}
for _, port := range svc.Spec.Ports {
if err := checkNodePort(validator.cluster, validator.appname, int(port.NodePort)); err != nil {
return err
}
}
return nil
}
func checkNodePort(cluster, newAppname string, nodePort int) error {
// TODO: refactor!!! NodePort is used by application verify
return nil
}