Skip to content

Commit

Permalink
cluster: add a connection status field (#5634)
Browse files Browse the repository at this point in the history
* cluster: add a connection status field

* response to comments
  • Loading branch information
nicks authored Mar 29, 2022
1 parent 78767fd commit 6c1a395
Show file tree
Hide file tree
Showing 15 changed files with 1,820 additions and 985 deletions.
34 changes: 17 additions & 17 deletions internal/cli/wire_gen.go

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

1 change: 1 addition & 0 deletions internal/controllers/core/cluster/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type connection struct {
createdAt time.Time
arch string
registry *container.Registry
connStatus *v1alpha1.ClusterConnectionStatus
}

func (k *ConnectionManager) GetK8sClient(clusterKey types.NamespacedName) (k8s.Client, metav1.MicroTime, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/core/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func KubernetesClientFromEnv(ctx context.Context, contextOverride k8s.KubeContex
return nil, err
}
minikubeClient := k8s.ProvideMinikubeClient(kubeContext)
client := k8s.ProvideK8sClient(ctx, env, restConfigOrError, clientsetOrError, portForwardClient, namespace, minikubeClient, clientConfig)
client := k8s.ProvideK8sClient(ctx, env, restConfigOrError, clientsetOrError, portForwardClient, kubeContext, namespace, minikubeClient, clientConfig)
_, err = client.CheckConnected(ctx)
if err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions internal/controllers/core/cluster/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
conn.registry = &reg
}

if conn.error == "" && conn.connType == connectionTypeK8s {
connStatus := conn.k8sClient.ConnectionConfig()
conn.connStatus = &v1alpha1.ClusterConnectionStatus{
Kubernetes: connStatus,
}
}

r.connManager.store(nn, conn)

status := conn.toStatus()
Expand Down Expand Up @@ -259,5 +266,6 @@ func (c *connection) toStatus() v1alpha1.ClusterStatus {
Arch: c.arch,
ConnectedAt: connectedAt,
Registry: reg,
Connection: c.connStatus,
}
}
23 changes: 23 additions & 0 deletions internal/controllers/core/cluster/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ func TestKubernetesArch(t *testing.T) {
assert.ElementsMatch(t, []analytics.CountEvent{connectEvt}, f.ma.Counts)
}

func TestKubernetesConnStatus(t *testing.T) {
f := newFixture(t)
cluster := &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "default"},
Spec: v1alpha1.ClusterSpec{
Connection: &v1alpha1.ClusterConnection{
Kubernetes: &v1alpha1.KubernetesClusterConnection{},
},
},
}

nn := types.NamespacedName{Name: "default"}
f.Create(cluster)
f.MustGet(nn, cluster)
assert.Equal(t, &v1alpha1.ClusterConnectionStatus{
Kubernetes: &v1alpha1.KubernetesClusterConnectionStatus{
Context: "default",
Namespace: "default",
Product: "unknown",
},
}, cluster.Status.Connection)
}

func TestDockerError(t *testing.T) {
f := newFixture(t)
cluster := &v1alpha1.Cluster{
Expand Down
14 changes: 14 additions & 0 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

"github.com/tilt-dev/clusterid"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
)

Expand Down Expand Up @@ -135,6 +136,8 @@ type Client interface {
OwnerFetcher() OwnerFetcher

ClusterHealth(ctx context.Context, verbose bool) (ClusterHealth, error)

ConnectionConfig() *v1alpha1.KubernetesClusterConnectionStatus
}

type RESTMapper interface {
Expand All @@ -149,6 +152,7 @@ type K8sClient struct {
core apiv1.CoreV1Interface
restConfig *rest.Config
portForwardClient PortForwardClient
configContext KubeContext
configNamespace Namespace
clientset kubernetes.Interface
discovery discovery.CachedDiscoveryInterface
Expand All @@ -171,6 +175,7 @@ func ProvideK8sClient(
maybeRESTConfig RESTConfigOrError,
maybeClientset ClientsetOrError,
pfClient PortForwardClient,
configContext KubeContext,
configNamespace Namespace,
mkClient MinikubeClient,
clientLoader clientcmd.ClientConfig) Client {
Expand Down Expand Up @@ -221,6 +226,7 @@ func ProvideK8sClient(
restConfig: restConfig,
portForwardClient: pfClient,
discovery: discovery,
configContext: configContext,
configNamespace: configNamespace,
clientset: clientset,
runtimeAsync: runtimeAsync,
Expand Down Expand Up @@ -355,6 +361,14 @@ func (k *K8sClient) OwnerFetcher() OwnerFetcher {
return k.ownerFetcher
}

func (k *K8sClient) ConnectionConfig() *v1alpha1.KubernetesClusterConnectionStatus {
return &v1alpha1.KubernetesClusterConnectionStatus{
Context: string(k.configContext),
Namespace: k.configNamespace.String(),
Product: string(k.product),
}
}

// Update an entity like kubectl apply does.
//
// This is the "best" way to apply a change.
Expand Down
8 changes: 8 additions & 0 deletions internal/k8s/exploding_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"

"github.com/tilt-dev/clusterid"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)

Expand Down Expand Up @@ -102,3 +104,9 @@ func (ec *explodingClient) OwnerFetcher() OwnerFetcher {
func (ec *explodingClient) ClusterHealth(_ context.Context, _ bool) (ClusterHealth, error) {
return ClusterHealth{}, errors.Wrap(ec.err, "could not set up kubernetes client")
}

func (ec *explodingClient) ConnectionConfig() *v1alpha1.KubernetesClusterConnectionStatus {
return &v1alpha1.KubernetesClusterConnectionStatus{
Product: string(clusterid.ProductUnknown),
}
}
10 changes: 10 additions & 0 deletions internal/k8s/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"

"github.com/tilt-dev/clusterid"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
)

Expand Down Expand Up @@ -520,6 +522,14 @@ func (c *FakeK8sClient) ContainerLogs(ctx context.Context, pID PodID, cName cont
return ReaderCloser{Reader: r}, nil
}

func (c *FakeK8sClient) ConnectionConfig() *v1alpha1.KubernetesClusterConnectionStatus {
return &v1alpha1.KubernetesClusterConnectionStatus{
Context: "default",
Namespace: "default",
Product: string(clusterid.ProductUnknown),
}
}

func (c *FakeK8sClient) ClusterHealth(_ context.Context, _ bool) (ClusterHealth, error) {
c.mu.Lock()
defer c.mu.Unlock()
Expand Down
26 changes: 26 additions & 0 deletions pkg/apis/core/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ type ClusterStatus struct {
//
// +optional
Registry *RegistryHosting `json:"registry,omitempty" protobuf:"bytes,4,opt,name=registry"`

// Connection status for an existing cluster.
//
// +optional
Connection *ClusterConnectionStatus `json:"connection,omitempty" protobuf:"bytes,5,opt,name=connection"`
}

type RegistryHosting struct {
Expand Down Expand Up @@ -231,3 +236,24 @@ var _ resource.StatusSubResource = &ClusterStatus{}
func (in ClusterStatus) CopyTo(parent resource.ObjectWithStatusSubResource) {
parent.(*Cluster).Status = in
}

// Connection spec for an existing cluster.
type ClusterConnectionStatus struct {
// Defines connection to a Kubernetes cluster.
Kubernetes *KubernetesClusterConnectionStatus `json:"kubernetes,omitempty" protobuf:"bytes,1,opt,name=kubernetes"`
}

// Kubernetes-specific fields for connection status
type KubernetesClusterConnectionStatus struct {
// The resolved kubeconfig context.
Context string `json:"context" protobuf:"bytes,2,opt,name=context"`

// The resolved default namespace.
Namespace string `json:"namespace" protobuf:"bytes,3,opt,name=namespace"`

// The product name for this cluster.
//
// For a complete list of possible product names, see:
// https://pkg.go.dev/github.com/tilt-dev/clusterid#Product
Product string `json:"product,omitempty" protobuf:"bytes,1,opt,name=product"`
}
Loading

0 comments on commit 6c1a395

Please sign in to comment.