Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 47b4945

Browse files
committed
Merge pull request #43 in DEV/k8s-argus from DEV-58267-change-deployment-client-to-apps-v1-apps-v1betv2-is-no-more-could-not-list-deployments to feature/upgrade
* commit '753ff7ac22ea8517204025937da6f7acc494f207': DEV-58267: updated v1betv2 client to v1 for deployment watcher.
2 parents b628107 + 753ff7a commit 47b4945

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

pkg/argus.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ func getK8sRESTClient(clientset *kubernetes.Clientset, apiVersion string) rest.I
208208
return clientset.CoreV1().RESTClient()
209209
case constants.K8sAPIVersionAppsV1beta2:
210210
return clientset.AppsV1beta2().RESTClient()
211+
case constants.K8sAPIVersionAppsV1:
212+
return clientset.AppsV1().RESTClient()
211213
default:
212214
return clientset.CoreV1().RESTClient()
213215
}

pkg/constants/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,6 @@ const (
101101
K8sAPIVersionAppsV1beta1 = "apps/v1beta1"
102102
// K8sAPIVersionAppsV1beta2 is the version 'apps/v1beta2' of k8s api
103103
K8sAPIVersionAppsV1beta2 = "apps/v1beta2"
104+
// K8sAPIVersionAppsV1 is the version 'apps/v1' of k8s api
105+
K8sAPIVersionAppsV1 = "apps/v1"
104106
)

pkg/permission/permission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func HasDeploymentPermissions() bool {
2626
if deploymentPermissionFlag != nil {
2727
return *deploymentPermissionFlag
2828
}
29-
_, err := client.AppsV1beta2().Deployments(v1.NamespaceAll).List(metav1.ListOptions{})
29+
_, err := client.AppsV1().Deployments(v1.NamespaceAll).List(metav1.ListOptions{})
3030
if err != nil {
3131
deploymentPermissionFlag = &disable
3232
log.Errorf("Failed to list deployments: %+v", err)

pkg/watch/deployment/deployment.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/logicmonitor/k8s-argus/pkg/permission"
1111
"github.com/logicmonitor/k8s-argus/pkg/types"
1212
log "github.com/sirupsen/logrus"
13-
"k8s.io/api/apps/v1beta2"
13+
appsv1 "k8s.io/api/apps/v1"
1414
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
"k8s.io/apimachinery/pkg/runtime"
1616
"k8s.io/client-go/kubernetes"
@@ -27,7 +27,7 @@ type Watcher struct {
2727

2828
// APIVersion is a function that implements the Watcher interface.
2929
func (w *Watcher) APIVersion() string {
30-
return constants.K8sAPIVersionAppsV1beta2
30+
return constants.K8sAPIVersionAppsV1
3131
}
3232

3333
// Enabled is a function that check the resource can watch.
@@ -42,13 +42,13 @@ func (w *Watcher) Resource() string {
4242

4343
// ObjType is a function that implements the Watcher interface.
4444
func (w *Watcher) ObjType() runtime.Object {
45-
return &v1beta2.Deployment{}
45+
return &appsv1.Deployment{}
4646
}
4747

4848
// AddFunc is a function that implements the Watcher interface.
4949
func (w *Watcher) AddFunc() func(obj interface{}) {
5050
return func(obj interface{}) {
51-
deployment := obj.(*v1beta2.Deployment)
51+
deployment := obj.(*appsv1.Deployment)
5252
log.Infof("Handling add deployment event: %s", deployment.Name)
5353
w.add(deployment)
5454
}
@@ -57,8 +57,8 @@ func (w *Watcher) AddFunc() func(obj interface{}) {
5757
// UpdateFunc is a function that implements the Watcher interface.
5858
func (w *Watcher) UpdateFunc() func(oldObj, newObj interface{}) {
5959
return func(oldObj, newObj interface{}) {
60-
old := oldObj.(*v1beta2.Deployment)
61-
new := newObj.(*v1beta2.Deployment)
60+
old := oldObj.(*appsv1.Deployment)
61+
new := newObj.(*appsv1.Deployment)
6262

6363
w.update(old, new)
6464
}
@@ -67,7 +67,7 @@ func (w *Watcher) UpdateFunc() func(oldObj, newObj interface{}) {
6767
// DeleteFunc is a function that implements the Watcher interface.
6868
func (w *Watcher) DeleteFunc() func(obj interface{}) {
6969
return func(obj interface{}) {
70-
deployment := obj.(*v1beta2.Deployment)
70+
deployment := obj.(*appsv1.Deployment)
7171
log.Debugf("Handling delete deployment event: %s", deployment.Name)
7272
// Delete the deployment.
7373
if w.Config().DeleteDevices {
@@ -85,7 +85,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
8585
}
8686

8787
// nolint: dupl
88-
func (w *Watcher) add(deployment *v1beta2.Deployment) {
88+
func (w *Watcher) add(deployment *appsv1.Deployment) {
8989
if _, err := w.Add(
9090
w.args(deployment, constants.DeploymentCategory)...,
9191
); err != nil {
@@ -95,7 +95,7 @@ func (w *Watcher) add(deployment *v1beta2.Deployment) {
9595
log.Infof("Added deployment %q", fmtDeploymentDisplayName(deployment))
9696
}
9797

98-
func (w *Watcher) update(old, new *v1beta2.Deployment) {
98+
func (w *Watcher) update(old, new *appsv1.Deployment) {
9999
if _, err := w.UpdateAndReplaceByDisplayName(
100100
fmtDeploymentDisplayName(old),
101101
w.args(new, constants.DeploymentCategory)...,
@@ -106,15 +106,15 @@ func (w *Watcher) update(old, new *v1beta2.Deployment) {
106106
log.Infof("Updated deployment %q", fmtDeploymentDisplayName(old))
107107
}
108108

109-
func (w *Watcher) move(deployment *v1beta2.Deployment) {
109+
func (w *Watcher) move(deployment *appsv1.Deployment) {
110110
if _, err := w.UpdateAndReplaceFieldByDisplayName(fmtDeploymentDisplayName(deployment), constants.CustomPropertiesFieldName, w.args(deployment, constants.DeploymentDeletedCategory)...); err != nil {
111111
log.Errorf("Failed to move deployment %q: %v", fmtDeploymentDisplayName(deployment), err)
112112
return
113113
}
114114
log.Infof("Moved deployment %q", fmtDeploymentDisplayName(deployment))
115115
}
116116

117-
func (w *Watcher) args(deployment *v1beta2.Deployment, category string) []types.DeviceOption {
117+
func (w *Watcher) args(deployment *appsv1.Deployment, category string) []types.DeviceOption {
118118
return []types.DeviceOption{
119119
w.Name(deployment.Name),
120120
w.ResourceLabels(deployment.Labels),
@@ -130,14 +130,14 @@ func (w *Watcher) args(deployment *v1beta2.Deployment, category string) []types.
130130
}
131131

132132
// fmtDeploymentDisplayName implements the conversion for the deployment display name
133-
func fmtDeploymentDisplayName(deployment *v1beta2.Deployment) string {
133+
func fmtDeploymentDisplayName(deployment *appsv1.Deployment) string {
134134
return fmt.Sprintf("%s.%s.deploy-%s", deployment.Name, deployment.Namespace, string(deployment.UID))
135135
}
136136

137137
// GetDeploymentsMap implements the getting deployments map info from k8s
138138
func GetDeploymentsMap(k8sClient *kubernetes.Clientset, namespace string) (map[string]string, error) {
139139
deploymentsMap := make(map[string]string)
140-
deploymentList, err := k8sClient.AppsV1beta2().Deployments(namespace).List(v1.ListOptions{})
140+
deploymentList, err := k8sClient.AppsV1().Deployments(namespace).List(v1.ListOptions{})
141141
if err != nil || deploymentList == nil {
142142
log.Warnf("Failed to get the deployments from k8s")
143143
return nil, err

0 commit comments

Comments
 (0)