Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Add rukpakctl subcommands that utilize local source bundle capabilities.
Browse files Browse the repository at this point in the history
rukpakctl create bundle <bundle name> <manifest directory> - create bundle with the contents of the manifest directory
rukpakctl create bundledeployment <bundledeployment name> <manifest directory> - create bundledeployment with the contents of the manifest directory
rukpakctl content <bundle name> - show the contents of the bundle

Signed-off-by: akihikokuroda <akuroda@us.ibm.com>
  • Loading branch information
akihikokuroda committed Jul 11, 2022
1 parent 10bea78 commit 8ece30a
Show file tree
Hide file tree
Showing 6 changed files with 562 additions and 40 deletions.
131 changes: 131 additions & 0 deletions cmd/rukpakctl/cmd/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
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 cmd

import (
"context"
"errors"
"fmt"
"path/filepath"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"github.com/operator-framework/rukpak/cmd/rukpakctl/utils"
)

type BundleOptions struct {
k8sclient *kubernetes.Clientset
rtclient runtimeclient.Client
namespace string
}

var bundleOpt BundleOptions

// bundleCmd represents the bundle command
var bundleCmd = &cobra.Command{
Use: "bundle <bundle name>, <manifest directory>",
Short: "create rukpak bundle resource.",
Long: `create rukpak bundle resource with specified contents.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("requires 2 arguments: <bundle name>, <manifest file directory>")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
err := bundle(bundleOpt, args)
if err != nil {
fmt.Printf("bundle command failed: %+v\n", err)
}
},
}

func init() {
createCmd.AddCommand(bundleCmd)

var kubeconfig string
if home := homedir.HomeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
} else {
kubeconfig = filepath.Join("~", ".kube", "config")
}

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}

scheme := runtime.NewScheme()
err = rukpakv1alpha1.AddToScheme(scheme)
if err != nil {
fmt.Printf("failed to add schema: %+v\n", err)
}
bundleOpt.rtclient, err = runtimeclient.New(config, runtimeclient.Options{
Scheme: scheme,
})
if err != nil {
fmt.Printf("failed to create kubernetes client: %+v\n", err)
}

if bundleOpt.k8sclient, err = kubernetes.NewForConfig(config); err != nil {
fmt.Printf("failed to create kubernetes client: %+v\n", err)
}
bundleOpt.namespace = "rukpak-system"
}

func bundle(opt BundleOptions, args []string) error {
// Create a bundle configmap
configmapName, err := utils.CreateConfigmap(opt.k8sclient, args[0], args[1], opt.namespace)
if err != nil {
return fmt.Errorf("failed to create a configmap: %+v", err)
}

bundle := &rukpakv1alpha1.Bundle{
TypeMeta: metav1.TypeMeta{
Kind: "Bundle",
APIVersion: "core.rukpak.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: args[0],
},
Spec: rukpakv1alpha1.BundleSpec{
ProvisionerClassName: "core.rukpak.io/plain",
Source: rukpakv1alpha1.BundleSource{
Type: rukpakv1alpha1.SourceTypeLocal,
Local: &rukpakv1alpha1.LocalSource{
ConfigMapRef: &rukpakv1alpha1.ConfigMapRef{
Name: configmapName,
Namespace: opt.namespace,
},
},
},
},
}
err = opt.rtclient.Create(context.Background(), bundle)
if err != nil {
return fmt.Errorf("failed to create bundle: %+v", err)
}

return nil
}
136 changes: 136 additions & 0 deletions cmd/rukpakctl/cmd/bundledeployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
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 cmd

import (
"context"
"errors"
"fmt"
"path/filepath"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"github.com/operator-framework/rukpak/cmd/rukpakctl/utils"
)

type BundleDeploymentOptions struct {
k8sclient *kubernetes.Clientset
rtclient runtimeclient.Client
namespace string
}

var bundleDeploymentOpt BundleDeploymentOptions

// contentCmd represents the content command
var bundledeploymentCmd = &cobra.Command{
Use: "bundledeployment <bundledeployment name> <manifest directory>",
Short: "create rukpak bundledeployment resource",
Long: `create rukpak bundledeployment resource with specified contents.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("requires 2 arguments: <bundle name>, <manifest file directory>")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
err := bundleDeployment(bundleDeploymentOpt, args)
if err != nil {
fmt.Printf("bundledeployment command failed: %+v\n", err)
}
},
}

func init() {
createCmd.AddCommand(bundledeploymentCmd)

var kubeconfig string
if home := homedir.HomeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
} else {
kubeconfig = filepath.Join("~", ".kube", "config")
}

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}

scheme := runtime.NewScheme()
err = rukpakv1alpha1.AddToScheme(scheme)
if err != nil {
fmt.Printf("failed to add schema: %+v\n", err)
}
bundleDeploymentOpt.rtclient, err = runtimeclient.New(config, runtimeclient.Options{
Scheme: scheme,
})
if err != nil {
fmt.Printf("failed to create kubernetes client: %+v\n", err)
}

if bundleDeploymentOpt.k8sclient, err = kubernetes.NewForConfig(config); err != nil {
fmt.Printf("failed to create kubernetes client: %+v\n", err)
}
bundleDeploymentOpt.namespace = "rukpak-system"
}

func bundleDeployment(opt BundleDeploymentOptions, args []string) error {
// Create a bundle configmap
configmapName, err := utils.CreateConfigmap(opt.k8sclient, args[0], args[1], opt.namespace)
if err != nil {
return fmt.Errorf("failed to create a configmap: %+v", err)
}

bundleDeployment := &rukpakv1alpha1.BundleDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "BundleDeployment",
APIVersion: "core.rukpak.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: args[0],
},
Spec: rukpakv1alpha1.BundleDeploymentSpec{
ProvisionerClassName: "core.rukpak.io/plain",
Template: &rukpakv1alpha1.BundleTemplate{
Spec: rukpakv1alpha1.BundleSpec{
ProvisionerClassName: "core.rukpak.io/plain",
Source: rukpakv1alpha1.BundleSource{
Type: rukpakv1alpha1.SourceTypeLocal,
Local: &rukpakv1alpha1.LocalSource{
ConfigMapRef: &rukpakv1alpha1.ConfigMapRef{
Name: configmapName,
Namespace: opt.namespace,
},
},
},
},
},
},
}
err = opt.rtclient.Create(context.Background(), bundleDeployment)
if err != nil {
return fmt.Errorf("failed to create bundledeployment: %+v", err)
}

return nil
}
Loading

0 comments on commit 8ece30a

Please sign in to comment.