Skip to content

Commit

Permalink
cluster: print current cluster, like kubectl config get-contexts (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks authored Nov 9, 2020
1 parent 0489474 commit 80fefee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type ClusterStatus struct {

// The number of CPU. Only applicable to local clusters.
CPUs int `json:"cpus,omitempty" yaml:"cpus,omitempty"`

// Whether this is the current cluster in `kubectl`
Current bool `json:"current,omitempty" yaml:"current,omitempty"`
}

// ClusterList is a list of Clusters.
Expand Down
9 changes: 9 additions & 0 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ func (c *Controller) configCopy() *clientcmdapi.Config {
return c.config.DeepCopy()
}

func (c *Controller) configCurrent() string {
c.mu.Lock()
defer c.mu.Unlock()

return c.config.CurrentContext
}

func (c *Controller) client(name string) (kubernetes.Interface, error) {
c.mu.Lock()
defer c.mu.Unlock()
Expand Down Expand Up @@ -303,6 +310,8 @@ func (c *Controller) populateCluster(ctx context.Context, cluster *api.Cluster)
}()

wg.Wait()

cluster.Status.Current = c.configCurrent() == cluster.Name
}

func FillDefaults(cluster *api.Cluster) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func (o *GetOptions) clustersAsTable(clusters []api.Cluster) runtime.Object {
table := metav1.Table{
TypeMeta: metav1.TypeMeta{Kind: "Table", APIVersion: "metav1.k8s.io"},
ColumnDefinitions: []metav1.TableColumnDefinition{
metav1.TableColumnDefinition{
Name: "Current",
Type: "string",
},
metav1.TableColumnDefinition{
Name: "Name",
Type: "string",
Expand Down Expand Up @@ -214,8 +218,14 @@ func (o *GetOptions) clustersAsTable(clusters []api.Cluster) runtime.Object {
rHost = "none"
}

current := ""
if cluster.Status.Current {
current = "*"
}

table.Rows = append(table.Rows, metav1.TableRow{
Cells: []interface{}{
current,
cluster.Name,
cluster.Product,
age,
Expand Down
8 changes: 5 additions & 3 deletions pkg/cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var clusterList = &api.ClusterList{
Product: "microk8s",
Status: api.ClusterStatus{
CreationTimestamp: metav1.Time{Time: createTime},
Current: true,
},
},
api.Cluster{
Expand All @@ -49,9 +50,9 @@ func TestDefaultPrint(t *testing.T) {

err := o.Print(o.transformForOutput(clusterList))
require.NoError(t, err)
assert.Equal(t, out.String(), `NAME PRODUCT AGE REGISTRY
microk8s microk8s 3y none
kind-kind KIND 3y localhost:5000
assert.Equal(t, out.String(), `CURRENT NAME PRODUCT AGE REGISTRY
* microk8s microk8s 3y none
kind-kind KIND 3y localhost:5000
`)
}

Expand All @@ -73,6 +74,7 @@ items:
product: microk8s
status:
creationTimestamp: "2017-07-14T02:40:00Z"
current: true
- apiVersion: ctlptl.dev/v1alpha1
kind: Cluster
name: kind-kind
Expand Down

0 comments on commit 80fefee

Please sign in to comment.