Skip to content

✨ support restconfig to target the control plane #756

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

Merged
merged 1 commit into from
Feb 21, 2020
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/internal/testing/integration/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package integration
import (
"fmt"
"net/url"

"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

// ControlPlane is a struct that knows how to start your test control plane.
Expand Down Expand Up @@ -57,3 +61,16 @@ func (f *ControlPlane) KubeCtl() *KubeCtl {
k.Opts = append(k.Opts, fmt.Sprintf("--server=%s", f.APIURL()))
return k
}

// RESTClientConfig returns a pre-configured restconfig, ready to connect to
// this ControlPlane.
func (f *ControlPlane) RESTClientConfig() (*rest.Config, error) {
c := &rest.Config{
Host: f.APIURL().String(),
ContentConfig: rest.ContentConfig{
NegotiatedSerializer: serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs},
},
}
err := rest.SetKubernetesDefaults(c)
return c, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration"
)

Expand Down Expand Up @@ -38,7 +44,24 @@ var _ = Describe("The Testing Framework", func() {
fmt.Sprintf("Expected Etcd to listen for clients on %s,", etcdClientURL.Host))

By("Ensuring APIServer is listening")
CheckAPIServerIsReady(controlPlane.KubeCtl())
c, err := controlPlane.RESTClientConfig()
Expect(err).NotTo(HaveOccurred())
CheckAPIServerIsReady(c)

By("getting a kubeclient & run it against the control plane")
c.APIPath = "/api"
c.ContentConfig.GroupVersion = &schema.GroupVersion{Version: "v1"}
kubeClient, err := rest.RESTClientFor(c)
Expect(err).NotTo(HaveOccurred())
result := &corev1.PodList{}
err = kubeClient.Get().
Namespace("default").
Resource("pods").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Into(result)
Expect(err).NotTo(HaveOccurred())
Expect(result.Items).To(BeEmpty())

By("getting a kubectl & run it against the control plane")
kubeCtl := controlPlane.KubeCtl()
Expand Down Expand Up @@ -165,26 +188,98 @@ func isSomethingListeningOnPort(hostAndPort string) portChecker {
// this changed behaviour does what it should do, we used the same test as in
// k/k's test-cmd (see link above) and test if certain well-known known APIs
// are actually available.
func CheckAPIServerIsReady(kubeCtl *integration.KubeCtl) {
expectedAPIS := []string{
"/api/v1/namespaces/default/pods 200 OK",
"/api/v1/namespaces/default/replicationcontrollers 200 OK",
"/api/v1/namespaces/default/services 200 OK",
"/apis/apps/v1/namespaces/default/daemonsets 200 OK",
"/apis/apps/v1/namespaces/default/deployments 200 OK",
"/apis/apps/v1/namespaces/default/replicasets 200 OK",
"/apis/apps/v1/namespaces/default/statefulsets 200 OK",
"/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers 200",
"/apis/batch/v1/namespaces/default/jobs 200 OK",
}

_, output, err := kubeCtl.Run("--v=6", "--namespace", "default", "get", "all", "--chunk-size=0")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

stdoutBytes, err := ioutil.ReadAll(output)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

for _, api := range expectedAPIS {
ExpectWithOffset(1, string(stdoutBytes)).To(ContainSubstring(api))
}
func CheckAPIServerIsReady(c *rest.Config) {
// check pods, replicationcontrollers and services
c.APIPath = "/api"
c.ContentConfig.GroupVersion = &schema.GroupVersion{Version: "v1"}
kubeClient, err := rest.RESTClientFor(c)
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("pods").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("replicationcontrollers").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("services").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

// check daemonsets, deployments, replicasets and statefulsets,
c.APIPath = "/apis"
c.ContentConfig.GroupVersion = &schema.GroupVersion{Group: "apps", Version: "v1"}
kubeClient, err = rest.RESTClientFor(c)
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("daemonsets").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("deployments").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("replicasets").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("statefulsets").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

// check horizontalpodautoscalers
c.ContentConfig.GroupVersion = &schema.GroupVersion{Group: "autoscaling", Version: "v1"}
kubeClient, err = rest.RESTClientFor(c)
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("horizontalpodautoscalers").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())

// check jobs
c.ContentConfig.GroupVersion = &schema.GroupVersion{Group: "batch", Version: "v1"}
kubeClient, err = rest.RESTClientFor(c)
Expect(err).NotTo(HaveOccurred())

_, err = kubeClient.Get().
Namespace("default").
Resource("jobs").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Do().
Get()
Expect(err).NotTo(HaveOccurred())
}