forked from glasskube/glasskube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
221a05a
commit 54a5237
Showing
9 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,5 @@ go.work | |
bin/ | ||
|
||
dist/ | ||
|
||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/glasskube/glasskube/api/v1alpha1/condition" | ||
"github.com/glasskube/glasskube/cmd/glasskube/config" | ||
"github.com/glasskube/glasskube/pkg/client" | ||
"github.com/glasskube/glasskube/pkg/install" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var installCmd = &cobra.Command{ | ||
Use: "install [package-name]", | ||
Short: "Install a package", | ||
Long: `Install a package.`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
pkgClient, err := client.InitKubeClient(config.Kubeconfig) | ||
if err != nil { | ||
return err | ||
} | ||
status, err := install.Install(pkgClient, cmd.Context(), args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
if status != nil { | ||
switch (*status).Status { | ||
case condition.Ready: | ||
fmt.Println("Installed successfully.") | ||
default: | ||
fmt.Printf("Installation has status %v, reason: %v\nMessage: %v\n", | ||
(*status).Status, (*status).Reason, (*status).Message) | ||
} | ||
} else { | ||
fmt.Println("Installation status unknown - no error and no status have been observed.") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(installCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package config | ||
|
||
var Kubeconfig string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/glasskube/glasskube/api/v1alpha1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
var PackageGVR = v1alpha1.GroupVersion.WithResource("packages") | ||
|
||
func InitKubeClient(kubeconfig string) (*PackageV1Alpha1Client, error) { | ||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() | ||
if kubeconfig != "" { | ||
loadingRules.ExplicitPath = kubeconfig | ||
} | ||
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) | ||
config, err := clientConfig.ClientConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = v1alpha1.AddToScheme(scheme.Scheme) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pkgClient, err := NewPackageClient(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pkgClient, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"github.com/glasskube/glasskube/api/v1alpha1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type PackageV1Alpha1Client struct { | ||
restClient rest.Interface | ||
} | ||
|
||
type PackageInterface interface { | ||
Create(ctx context.Context, p *v1alpha1.Package) error | ||
Watch(ctx context.Context) (watch.Interface, error) | ||
} | ||
|
||
type packageClient struct { | ||
restClient rest.Interface | ||
} | ||
|
||
func NewPackageClient(cfg *rest.Config) (*PackageV1Alpha1Client, error) { | ||
pkgRestConfig := *cfg | ||
pkgRestConfig.ContentConfig.GroupVersion = &v1alpha1.GroupVersion | ||
pkgRestConfig.APIPath = "/apis" | ||
pkgRestConfig.NegotiatedSerializer = scheme.Codecs.WithoutConversion() | ||
restClient, err := rest.RESTClientFor(&pkgRestConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PackageV1Alpha1Client{restClient: restClient}, err | ||
} | ||
|
||
func (c *PackageV1Alpha1Client) Packages() PackageInterface { | ||
return &packageClient{ | ||
restClient: c.restClient, | ||
} | ||
} | ||
|
||
func (c *packageClient) Create(ctx context.Context, pkg *v1alpha1.Package) error { | ||
return c.restClient.Post(). | ||
Resource(PackageGVR.Resource). | ||
Body(pkg).Do(ctx).Into(pkg) | ||
} | ||
|
||
func (c *packageClient) Watch(ctx context.Context) (watch.Interface, error) { | ||
opts := metav1.ListOptions{Watch: true} | ||
return c.restClient.Get(). | ||
Resource(PackageGVR.Resource). | ||
VersionedParams(&opts, scheme.ParameterCodec). | ||
Watch(ctx) | ||
} | ||
|
||
// NewPackage instantiates a new v1alpha1.Package struct with the given package name | ||
func NewPackage(packageName string) *v1alpha1.Package { | ||
return &v1alpha1.Package{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: packageName, | ||
}, | ||
Spec: v1alpha1.PackageSpec{ | ||
PackageInfo: v1alpha1.PackageInfoTemplate{ | ||
Name: packageName, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type PackageStatus struct { | ||
Status string | ||
Reason string | ||
Message string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package install | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/glasskube/glasskube/api/v1alpha1" | ||
"github.com/glasskube/glasskube/api/v1alpha1/condition" | ||
"github.com/glasskube/glasskube/pkg/client" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/watch" | ||
) | ||
|
||
// Install creates a new v1alpha1.Package custom resource in the cluster, and blocks until this resource has either | ||
// status Ready or Failed. | ||
func Install(pkgClient *client.PackageV1Alpha1Client, ctx context.Context, packageName string) (*client.PackageStatus, error) { | ||
pkg := client.NewPackage(packageName) | ||
err := pkgClient.Packages().Create(ctx, pkg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Printf("Installing %v.\n", packageName) | ||
|
||
status, err := awaitInstall(pkgClient, ctx, pkg.GetUID()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return status, nil | ||
} | ||
|
||
func awaitInstall(pkgClient *client.PackageV1Alpha1Client, ctx context.Context, pkgUID types.UID) (*client.PackageStatus, error) { | ||
watcher, err := pkgClient.Packages().Watch(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer watcher.Stop() | ||
for event := range watcher.ResultChan() { | ||
if obj, ok := event.Object.(*v1alpha1.Package); ok && obj.GetUID() == pkgUID { | ||
if event.Type == watch.Added || event.Type == watch.Modified { | ||
if status := getStatus(&obj.Status); status != nil { | ||
return status, nil | ||
} | ||
} else if event.Type == watch.Deleted { | ||
return nil, errors.New("created package has been deleted unexpectedly") | ||
} | ||
} | ||
} | ||
return nil, errors.New("failed to confirm package installation status") | ||
} | ||
|
||
func getStatus(status *v1alpha1.PackageStatus) *client.PackageStatus { | ||
readyCnd := meta.FindStatusCondition((*status).Conditions, condition.Ready) | ||
if readyCnd != nil && readyCnd.Status == v1.ConditionTrue { | ||
return newPackageStatus(readyCnd) | ||
} | ||
failedCnd := meta.FindStatusCondition((*status).Conditions, condition.Failed) | ||
if failedCnd != nil && failedCnd.Status == v1.ConditionTrue { | ||
return newPackageStatus(failedCnd) | ||
} | ||
return nil | ||
} | ||
|
||
func newPackageStatus(cnd *v1.Condition) *client.PackageStatus { | ||
return &client.PackageStatus{ | ||
Status: cnd.Type, | ||
Reason: cnd.Reason, | ||
Message: cnd.Message, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters