Skip to content

Commit

Permalink
fix: support kube 1.16+ api
Browse files Browse the repository at this point in the history
Signed-off-by: Jiang Yitao <[email protected]>
  • Loading branch information
jiangytcn authored and jenkins-x-bot committed Jan 2, 2020
1 parent 24fc7d9 commit a306c29
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
10 changes: 5 additions & 5 deletions pkg/applications/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"k8s.io/api/apps/v1beta1"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// Deployment represents an application deployment in a single environment
type Deployment struct {
*v1beta1.Deployment
*appsv1.Deployment
}

// Environment represents an environment in which an application has been
Expand Down Expand Up @@ -128,7 +128,7 @@ func GetApplications(factory clients.Factory) (List, error) {
kubeClient, _, err := factory.CreateKubeClient()

// fetch deployments by environment (excluding dev)
deployments := make(map[string]map[string]v1beta1.Deployment)
deployments := make(map[string]map[string]appsv1.Deployment)
for _, env := range permanentEnvsMap {
if env.Spec.Kind != v1.EnvironmentKindTypeDevelopment {
envDeployments, err := kube.GetDeployments(kubeClient, env.Spec.Namespace)
Expand All @@ -148,7 +148,7 @@ func GetApplications(factory clients.Factory) (List, error) {
return list, nil
}

func getDeploymentAppNameInEnvironment(d v1beta1.Deployment, e *v1.Environment) (string, error) {
func getDeploymentAppNameInEnvironment(d appsv1.Deployment, e *v1.Environment) (string, error) {
labels, err := metav1.LabelSelectorAsMap(d.Spec.Selector)
if err != nil {
return "", err
Expand All @@ -158,7 +158,7 @@ func getDeploymentAppNameInEnvironment(d v1beta1.Deployment, e *v1.Environment)
return name, nil
}

func (l List) appendMatchingDeployments(envs map[string]*v1.Environment, deps map[string]map[string]v1beta1.Deployment) error {
func (l List) appendMatchingDeployments(envs map[string]*v1.Environment, deps map[string]map[string]appsv1.Deployment) error {
for _, app := range l.Items {
for envName, env := range envs {
for _, dep := range deps[envName] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/get/get_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (o *GetEnvOptions) Run() error {

ens := env.Spec.Namespace
if ens != "" {
deps, err := kubeClient.AppsV1beta1().Deployments(ens).List(metav1.ListOptions{})
deps, err := kubeClient.AppsV1().Deployments(ens).List(metav1.ListOptions{})
if err != nil {
return fmt.Errorf("Could not find deployments in namespace %s: %s", ens, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/opts/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

// WaitForReadyPodForDeployment waits for a pod of a deployment to be ready
func (o *CommonOptions) WaitForReadyPodForDeployment(c kubernetes.Interface, ns string, name string, names []string, readyOnly bool) (string, error) {
deployment, err := c.AppsV1beta1().Deployments(ns).Get(name, metav1.GetOptions{})
deployment, err := c.AppsV1().Deployments(ns).Get(name, metav1.GetOptions{})
if err != nil || deployment == nil {
return "", util.InvalidArg(name, names)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/flagger/flagger.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package flagger

import (
"k8s.io/api/apps/v1beta1"
appsv1 "k8s.io/api/apps/v1"
)

// IsCanaryAuxiliaryDeployment returns whether this deployment has been created automatically by Flagger from a Canary object
func IsCanaryAuxiliaryDeployment(d v1beta1.Deployment) bool {
func IsCanaryAuxiliaryDeployment(d appsv1.Deployment) bool {
ownerReferences := d.GetObjectMeta().GetOwnerReferences()
for i := range ownerReferences {
if ownerReferences[i].Kind == "Canary" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestCreateOrUpdateActivities(t *testing.T) {
Name: kube.DeploymentTektonController,
},
}
mockKubeClient.AppsV1beta1().Deployments(nsObj.Namespace).Create(mockTektonDeployment)
mockKubeClient.AppsV1().Deployments(nsObj.Namespace).Create(mockTektonDeployment)
jxClient := jxfake.NewSimpleClientset()

const (
Expand Down
22 changes: 15 additions & 7 deletions pkg/kube/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/jenkins-x/jx/pkg/log"
"k8s.io/api/apps/v1beta1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -17,9 +17,10 @@ import (
tools_watch "k8s.io/client-go/tools/watch"
)

func GetDeployments(kubeClient kubernetes.Interface, ns string) (map[string]v1beta1.Deployment, error) {
answer := map[string]v1beta1.Deployment{}
deps, err := kubeClient.AppsV1beta1().Deployments(ns).List(metav1.ListOptions{})
// GetDeployments get deployments in the given namespace
func GetDeployments(kubeClient kubernetes.Interface, ns string) (map[string]appsv1.Deployment, error) {
answer := map[string]appsv1.Deployment{}
deps, err := kubeClient.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return answer, err
}
Expand All @@ -29,9 +30,10 @@ func GetDeployments(kubeClient kubernetes.Interface, ns string) (map[string]v1be
return answer, nil
}

// GetDeploymentNames get deployment names in the given namespace with filter
func GetDeploymentNames(client kubernetes.Interface, ns string, filter string) ([]string, error) {
names := []string{}
list, err := client.AppsV1beta1().Deployments(ns).List(metav1.ListOptions{})
list, err := client.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return names, fmt.Errorf("Failed to load Deployments %s", err)
}
Expand All @@ -45,8 +47,9 @@ func GetDeploymentNames(client kubernetes.Interface, ns string, filter string) (
return names, nil
}

func GetDeploymentByRepo(client kubernetes.Interface, ns string, repoName string) (*v1beta1.Deployment, error) {
deps, err := client.AppsV1beta1().Deployments(ns).List(metav1.ListOptions{})
// GetDeploymentByRepo get deployment in the given namespace with repo name
func GetDeploymentByRepo(client kubernetes.Interface, ns string, repoName string) (*appsv1.Deployment, error) {
deps, err := client.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
Expand All @@ -58,6 +61,7 @@ func GetDeploymentByRepo(client kubernetes.Interface, ns string, repoName string
return nil, fmt.Errorf("no deployment found for repository name '%s'", repoName)
}

// IsDeploymentRunning returns whether this deployment is running
func IsDeploymentRunning(client kubernetes.Interface, name, namespace string) (bool, error) {
options := metav1.GetOptions{}

Expand All @@ -72,6 +76,7 @@ func IsDeploymentRunning(client kubernetes.Interface, name, namespace string) (b
return false, nil
}

// WaitForAllDeploymentsToBeReady waits for the pods of all deployment to become ready in the given namespace
func WaitForAllDeploymentsToBeReady(client kubernetes.Interface, namespace string, timeoutPerDeploy time.Duration) error {
deployList, err := client.AppsV1().Deployments(namespace).List(metav1.ListOptions{})
if err != nil {
Expand All @@ -90,6 +95,7 @@ func WaitForAllDeploymentsToBeReady(client kubernetes.Interface, namespace strin
return nil
}

// WaitForDeploymentToBeCreatedAndReady waits for the pods of a deployment created and to become ready
func WaitForDeploymentToBeCreatedAndReady(client kubernetes.Interface, name, namespace string, timeoutPerDeploy time.Duration) error {

options := metav1.ListOptions{FieldSelector: fmt.Sprintf("metadata.name=%s", name)}
Expand Down Expand Up @@ -151,6 +157,7 @@ func WaitForDeploymentToBeReady(client kubernetes.Interface, name, namespace str
return nil
}

// DeploymentPodCount returns pod counts of deployment
func DeploymentPodCount(client kubernetes.Interface, name, namespace string) (int, error) {
pods, err := GetDeploymentPods(client, name, namespace)
if err == nil {
Expand All @@ -159,6 +166,7 @@ func DeploymentPodCount(client kubernetes.Interface, name, namespace string) (in
return 0, err
}

// GetDeploymentPods returns pods of deployment
func GetDeploymentPods(client kubernetes.Interface, name, namespace string) ([]v1.Pod, error) {
d, err := client.ExtensionsV1beta1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func GetEnrichedDevEnvironment(kubeClient kubernetes.Interface, jxClient version
// IsProwEnabled returns true if Prow is enabled in the given development namespace
func IsProwEnabled(kubeClient kubernetes.Interface, ns string) (bool, error) {
// lets try determine if its Jenkins or not via the deployments
_, err := kubeClient.AppsV1beta1().Deployments(ns).Get(DeploymentProwBuild, metav1.GetOptions{})
_, err := kubeClient.AppsV1().Deployments(ns).Get(DeploymentProwBuild, metav1.GetOptions{})
if err != nil {
if isProwBuildNotFoundError(err) {
return false, nil
Expand All @@ -128,7 +128,7 @@ func isProwBuildNotFoundError(err error) bool {
// IsTektonEnabled returns true if Build Pipeline is enabled in the given development namespace
func IsTektonEnabled(kubeClient kubernetes.Interface, ns string) (bool, error) {
// lets try determine if its Jenkins or not via the deployments
_, err := kubeClient.AppsV1beta1().Deployments(ns).Get(DeploymentTektonController, metav1.GetOptions{})
_, err := kubeClient.AppsV1().Deployments(ns).Get(DeploymentTektonController, metav1.GetOptions{})
if err != nil {
if isTektonNotFoundError(err) {
return false, nil
Expand Down

0 comments on commit a306c29

Please sign in to comment.