Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Kubectl plugin for service catalog #840

Closed
Closed
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
38 changes: 37 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ NON_VENDOR_DIRS = $(shell $(DOCKER_CMD) glide nv)
#########################################################################
build: .init .generate_files \
$(BINDIR)/service-catalog \
$(BINDIR)/user-broker
$(BINDIR)/user-broker \
plugins

user-broker: $(BINDIR)/user-broker
$(BINDIR)/user-broker: .init contrib/cmd/user-broker \
Expand Down Expand Up @@ -386,3 +387,38 @@ release-push-%:
$(MAKE) clean-bin
$(MAKE) ARCH=$* build
$(MAKE) ARCH=$* push


# kubectl plugin stuff
######################
PLUGIN_EXES=bind-service create-service-broker create-service-instance

plugins: .init .generate_files \
$(BINDIR)/bind-service/bind-service \
$(BINDIR)/create-service-broker/create-service-broker \
$(BINDIR)/create-service-instance/create-service-instance

$(BINDIR)/bind-service/bind-service: \
plugin/cmd/kubectl/bind-service/bind-service.go \
plugin/cmd/kubectl/bind-service/plugin.yaml
rm -rf $(BINDIR)/bind-service
$(DOCKER_CMD) $(GO_BUILD) -o $@ $<
$(DOCKER_CMD) cp plugin/cmd/kubectl/bind-service/*yaml \
$(BINDIR)/bind-service/

$(BINDIR)/create-service-broker/create-service-broker: \
plugin/cmd/kubectl/create-service-broker/create-service-broker.go \
plugin/cmd/kubectl/create-service-broker/plugin.yaml
rm -rf $(BINDIR)/create-service-broker
$(DOCKER_CMD) $(GO_BUILD) -o $@ $<
$(DOCKER_CMD) cp plugin/cmd/kubectl/create-service-broker/*yaml \
$(BINDIR)/create-service-broker/

$(BINDIR)/create-service-instance/create-service-instance: \
plugin/cmd/kubectl/create-service-instance/create-service-instance.go \
plugin/cmd/kubectl/create-service-instance/plugin.yaml
rm -rf $(BINDIR)/create-service-instance
$(DOCKER_CMD) $(GO_BUILD) -o $@ $<
$(DOCKER_CMD) cp plugin/cmd/kubectl/create-service-instance/*yaml \
$(BINDIR)/create-service-instance/

82 changes: 82 additions & 0 deletions plugin/cmd/kubectl/bind-service/bind-service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2016 The Kubernetes Authors.

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 main

import (
"fmt"
"os"

v1alpha1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1alpha1"
clientset "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset"
"github.com/kubernetes-incubator/service-catalog/plugin/cmd/kubectl/utils"

"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/rest"
)

const usage = `Usage:
kubectl plugin bind-service INSTANCE_NAME BINDING_NAME NAMESPACE`

func main() {
svcURL := utils.SCUrlEnv()

if len(os.Args) != 4 {
utils.Exit1(usage)
}

binding := v1alpha1.ServiceInstanceCredential{}
binding.Kind = "binding"
binding.Name = os.Args[2]
binding.Namespace = os.Args[3]
binding.Spec.ServiceInstanceRef = v1.LocalObjectReference{
Name: os.Args[1],
}
binding.Spec.SecretName = os.Args[2]

fmt.Printf("Looking up Namespace %s...\n", utils.Entity(binding.Namespace))
if err := utils.CheckNamespaceExists(binding.Namespace); err != nil {
utils.Exit1(err.Error())
}
utils.Ok()

restConfig := rest.Config{
Host: svcURL,
APIPath: "/apis/servicecatalog.k8s.io/v1alpha1",
}

svcClient, err := clientset.NewForConfig(&restConfig)
if err != nil {
utils.Exit1(fmt.Sprintf("Error initializing client for service catalog (%s)", err))
}

fmt.Printf("Creating binding %s to %s in Namespace %s...\n",
utils.Entity(binding.Name),
utils.Entity(binding.Spec.ServiceInstanceRef.Name),
utils.Entity(binding.Namespace))
resp, err := svcClient.ServiceInstanceCredentials(binding.Namespace).Create(&binding)
if err != nil {
utils.Exit1(fmt.Sprintf("Error binding service instance (%s)", err))
}
utils.Ok()

table := utils.NewTable("BINDING NAME", "NAMESPACE", "INSTANCE NAME", "SECRET NAME")
table.AddRow(resp.Name, resp.Namespace, resp.Spec.ServiceInstanceRef.Name, resp.Spec.SecretName)
err = table.Print()
if err != nil {
utils.Exit1(fmt.Sprintf("Error printing result (%s)", err))
}
}
3 changes: 3 additions & 0 deletions plugin/cmd/kubectl/bind-service/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: "bind-service"
shortDesc: "This command binds a service class to a service instance"
command: "./bind-service"
69 changes: 69 additions & 0 deletions plugin/cmd/kubectl/create-service-broker/create-service-broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2016 The Kubernetes Authors.

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 main

import (
"fmt"
"os"

v1alpha1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1alpha1"
clientset "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset"
"github.com/kubernetes-incubator/service-catalog/plugin/cmd/kubectl/utils"

"k8s.io/client-go/rest"
)

const usage = `Usage:
kubectl plugin create-service-broker BROKER_NAME BROKER_URL`

func main() {
svcURL := utils.SCUrlEnv()

if len(os.Args) != 3 {
utils.Exit1(usage)
}

broker := v1alpha1.ServiceBroker{}
broker.Kind = "Broker"
broker.Name = os.Args[1]
broker.Spec.URL = os.Args[2]

restConfig := rest.Config{
Host: svcURL,
APIPath: "/apis/servicecatalog.k8s.io/v1alpha1",
}

svcClient, err := clientset.NewForConfig(&restConfig)
if err != nil {
utils.Exit1(fmt.Sprintf("Initializing client for service catalog (%s)", err))
}

fmt.Printf("Creating broker %s...\n", utils.Entity(broker.Name))
resp, err := svcClient.ServiceBrokers().Create(&broker)
if err != nil {
utils.Exit1(fmt.Sprintf("Creating broker resource (%s)", err))
}

utils.Ok()

table := utils.NewTable("BROKER NAME", "NAMESPACE", "URL")
table.AddRow(resp.Name, resp.Namespace, resp.Spec.URL)
err = table.Print()
if err != nil {
utils.Exit1(fmt.Sprintf("Error printing result (%s)", err))
}
}
3 changes: 3 additions & 0 deletions plugin/cmd/kubectl/create-service-broker/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: "create-service-broker"
shortDesc: "This registers a service broker with the service catalog"
command: "./create-service-broker"
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2016 The Kubernetes Authors.

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 main

import (
"fmt"
"os"

v1alpha1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1alpha1"
clientset "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset"
"github.com/kubernetes-incubator/service-catalog/plugin/cmd/kubectl/utils"

"k8s.io/client-go/rest"
)

const usage = `Usage:
kubectl plugin create-service-instance SERVICE_CLASS_NAME PLAN_NAME INSTANCE_NAME NAMESPACE`

func main() {
svcURL := utils.SCUrlEnv()

if len(os.Args) != 5 {
utils.Exit1(usage)
}

instance := v1alpha1.ServiceInstance{}
instance.Kind = "Instance"
instance.Name = os.Args[3]
instance.Namespace = os.Args[4]
instance.Spec.PlanName = os.Args[2]
instance.Spec.ServiceClassName = os.Args[1]

fmt.Printf("Looking up Namespace %s...\n", utils.Entity(instance.Namespace))
if err := utils.CheckNamespaceExists(instance.Namespace); err != nil {
utils.Exit1(err.Error())
}
utils.Ok()

restConfig := rest.Config{
Host: svcURL,
APIPath: "/apis/servicecatalog.k8s.io/v1alpha1",
}

svcClient, err := clientset.NewForConfig(&restConfig)
if err != nil {
utils.Exit1(fmt.Sprintf("Failed to initializing client for service catalog (%s)", err))
}

fmt.Printf("Creating service instance %s in Namespace %s...\n", utils.Entity(instance.Name), utils.Entity(instance.Namespace))
resp, err := svcClient.ServiceInstances(instance.Namespace).Create(&instance)
if err != nil {
utils.Exit1(fmt.Sprintf("Failed to creating service instance (%s)", err))
}
utils.Ok()

table := utils.NewTable("INSTANCE NAME", "NAMESPACE", "PLAN NAME", "SERVICE CLASS NAME")
table.AddRow(resp.Name, resp.Namespace, resp.Spec.PlanName, resp.Spec.ServiceClassName)
err = table.Print()
if err != nil {
utils.Exit1(fmt.Sprintf("Error printing result (%s)", err))
}
}
3 changes: 3 additions & 0 deletions plugin/cmd/kubectl/create-service-instance/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: "create-service-instance"
shortDesc: "This command creates a service instance that is ready to be bound to"
command: "./create-service-instance"
67 changes: 67 additions & 0 deletions plugin/cmd/kubectl/utils/table_printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2016 The Kubernetes Authors.

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 utils

import (
"fmt"
"os"
"text/tabwriter"
)

// Table defines a tabular output - obviously in table format
type Table struct {
headers []string
rows [][]string
}

// NewTable creates a new table based on the passed in header names
func NewTable(headers ...string) *Table {
return &Table{
headers: headers,
}
}

// AddRow will append the specified row to the table
func (t *Table) AddRow(row ...string) {
t.rows = append(t.rows, row)
}

// Print prints the table to the screen
func (t *Table) Print() error {
padding := 3

w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', 0)

//Print header
printStr := ""
for _, h := range t.headers {
printStr = printStr + h + "\t"
}
fmt.Fprintln(w, printStr)

//Print rows
printStr = ""
for _, rows := range t.rows {
for _, row := range rows {
printStr = printStr + row + "\t"
}
fmt.Fprintln(w, printStr)
}
fmt.Fprintln(w)

return w.Flush()
}
Loading