Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kubernetes client libraries #51

Merged
merged 3 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
340 changes: 337 additions & 3 deletions Gopkg.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@
[[constraint]]
branch = "master"
name = "github.com/rjeczalik/notify"

[[constraint]]
name = "k8s.io/apimachinery"
revision = "kubernetes-1.9.2"

[[constraint]]
name = "k8s.io/client-go"
version = "kubernetes-1.9.2"

[[constraint]]
name = "k8s.io/api"
version = "kubernetes-1.9.2"
39 changes: 39 additions & 0 deletions pkg/skaffold/kubernetes/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2018 Google LLC

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 kubernetes

import (
"fmt"

"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

func GetClientset() (*kubernetes.Clientset, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
clientConfig, err := kubeConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("Error creating kubeConfig: %s", err)
}
client, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()")
}
return client, nil
}
51 changes: 51 additions & 0 deletions pkg/skaffold/kubernetes/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2018 Google LLC

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 kubernetes

import (
"fmt"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"

"k8s.io/apimachinery/pkg/util/wait"
)

func WaitForPodReady(pods corev1.PodInterface, podName string) error {
logrus.Infof("Waiting for %s to be ready", podName)
return wait.PollImmediate(time.Millisecond*500, time.Minute*10, func() (bool, error) {
pod, err := pods.Get(podName, meta_v1.GetOptions{
IncludeUninitialized: true,
})
if err != nil {
return false, errors.Wrap(err, "pod not found")
}
switch pod.Status.Phase {
case v1.PodRunning:
return true, nil
case v1.PodSucceeded, v1.PodFailed:
return false, fmt.Errorf("pod already in terminal phase: %s", pod.Status.Phase)
case v1.PodUnknown, v1.PodPending:
return false, nil
}
return false, fmt.Errorf("unknown phase: %s", pod.Status.Phase)
})
}
151 changes: 151 additions & 0 deletions pkg/skaffold/kubernetes/wait_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright 2018 Google LLC

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 kubernetes

import (
"testing"
"time"

"github.com/GoogleCloudPlatform/skaffold/testutil"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)

var podReadyState = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podname",
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container_name",
Image: "image_name",
},
},
},
}

var podUnitialized = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podname",
},
Status: v1.PodStatus{
Conditions: []v1.PodCondition{
{
Type: v1.PodScheduled,
},
},
Phase: v1.PodPending,
},
}

var podBadPhase = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podname",
},
Status: v1.PodStatus{
Conditions: []v1.PodCondition{
{
Type: v1.PodScheduled,
},
},
Phase: "not a real phase",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container_name",
Image: "image_name",
},
},
},
}

var podDifferentName = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod_different_name",
},
Status: v1.PodStatus{
Conditions: []v1.PodCondition{
{
Type: v1.PodScheduled,
},
},
Phase: "not a real phase",
},
}

func TestWaitForPodReady(t *testing.T) {
var tests = []struct {
description string
initialObj *v1.Pod
phases []v1.PodPhase
timeout time.Duration

shouldErr bool
}{
{
description: "pod already ready",
initialObj: podReadyState,
},
{
description: "pod uninitialized to succeed without running",
initialObj: podUnitialized,
phases: []v1.PodPhase{v1.PodUnknown, v1.PodSucceeded},
shouldErr: true,
},
{
description: "pod bad phase",
initialObj: podBadPhase,
shouldErr: true,
},
{
description: "pod not found",
initialObj: podDifferentName,
shouldErr: true,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
client := fake.NewSimpleClientset(test.initialObj)
pods := client.CoreV1().Pods("")
errCh := make(chan error, 1)
done := make(chan struct{}, 1)
go func() {
errCh <- WaitForPodReady(pods, "podname")
done <- struct{}{}
}()
for _, p := range test.phases {
time.Sleep(501 * time.Millisecond)
test.initialObj.Status.Phase = p
pods.UpdateStatus(test.initialObj)
}
_ = <-done
var err error
select {
case waitErr := <-errCh:
err = waitErr
default:
}
testutil.CheckError(t, test.shouldErr, err)
})
}
}
12 changes: 12 additions & 0 deletions vendor/github.com/PuerkitoBio/purell/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading