Skip to content

Commit

Permalink
updated to have release part of virtualenv
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Dec 20, 2023
1 parent 0dfe9b7 commit 1e6611b
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 59 deletions.
12 changes: 6 additions & 6 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var releaseCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
PreRun: setup,
Run: release,
Short: "Release specified AppDeployment and VirtualEnvironment",
Short: "Release specified AppDeployment and VirtualEnv",
Long: strings.TrimSpace(`
The release command activates the routes of the components belonging to the
specified AppDeployment. This causes genesis events matching components' routes
Expand All @@ -30,13 +30,13 @@ found you will be prompted to select the desired AppDeployment.
fox release main --virtual-env dev
# Release the AppDeployment with version 'v1.2.3' using the 'prod'
# VirtualEnvironment, creating an VirtualEnvironmentSnapshot if needed.
# VirtualEnv, creating an VirtualEnvSnapshot if needed.
fox release v1.2.3 --virtual-env prod --create-snapshot
`),
}

func init() {
releaseCmd.Flags().StringVarP(&cfg.Flags.VirtEnv, "virtual-env", "e", "", "name of ClusterVirtualEnvironment, VirtualEnvironment, or VirtualEnvironmentSnapshot to use")
releaseCmd.Flags().StringVarP(&cfg.Flags.VirtEnv, "virtual-env", "e", "", "name of VirtualEnv or VirtualEnvSnapshot to use")
releaseCmd.Flags().BoolVarP(&cfg.Flags.CreateVirtEnv, "create-snapshot", "c", false, "create an immutable snapshot of environment and use for release")

addCommonDeployFlags(releaseCmd)
Expand All @@ -50,9 +50,9 @@ func release(cmd *cobra.Command, args []string) {
appDep := args[0]
checkCommonDeployFlags(cfg.Flags.VirtEnv)

rel := repo.New(cfg).Release(appDep)
env := repo.New(cfg).Release(appDep)

// Makes output less cluttered.
rel.ManagedFields = nil
log.Marshal(rel)
env.ManagedFields = nil
log.Marshal(env)
}
2 changes: 1 addition & 1 deletion docs/fox.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ and release your KubeFox Apps.
* [fox init](fox_init.md) - Initialize a KubeFox App
* [fox proxy](fox_proxy.md) - Port forward local port to broker's HTTP server adapter
* [fox publish](fox_publish.md) - Builds, pushes, and deploys KubeFox Apps using the version of the currently checked out Git commit
* [fox release](fox_release.md) - Release specified AppDeployment and VirtualEnvironment
* [fox release](fox_release.md) - Release specified AppDeployment and VirtualEnv
* [fox version](fox_version.md) - Show version information of 🦊 Fox

6 changes: 3 additions & 3 deletions docs/fox_release.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## fox release

Release specified AppDeployment and VirtualEnvironment
Release specified AppDeployment and VirtualEnv

### Synopsis

Expand All @@ -25,7 +25,7 @@ fox release (NAME | COMMIT | SHORT COMMIT | VERSION | TAG | BRANCH) [flags]
fox release main --virtual-env dev
# Release the AppDeployment with version 'v1.2.3' using the 'prod'
# VirtualEnvironment, creating an VirtualEnvironmentSnapshot if needed.
# VirtualEnv, creating an VirtualEnvSnapshot if needed.
fox release v1.2.3 --virtual-env prod --create-snapshot
```

Expand All @@ -37,7 +37,7 @@ fox release v1.2.3 --virtual-env prod --create-snapshot
-h, --help help for release
-n, --namespace string namespace of KubeFox Platform
-p, --platform string name of KubeFox Platform to utilize
-e, --virtual-env string name of ClusterVirtualEnvironment, VirtualEnvironment, or VirtualEnvironmentSnapshot to use
-e, --virtual-env string name of VirtualEnv or VirtualEnvSnapshot to use
--wait duration wait up the specified time for components to be ready
```

Expand Down
2 changes: 1 addition & 1 deletion efs/hello-world/hack/environments/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: VirtualEnv
metadata:
name: prod
spec:
releasePolicy:
releasePolicies:
appDeploymentPolicy: VersionRequired
virtualEnvPolicy: SnapshotRequired
data:
Expand Down
2 changes: 1 addition & 1 deletion efs/hello-world/hack/environments/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: VirtualEnv
metadata:
name: qa
spec:
releasePolicy:
releasePolicies:
appDeploymentPolicy: VersionOptional
virtualEnvPolicy: SnapshotOptional
data:
Expand Down
1 change: 1 addition & 0 deletions hack/scripts/commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/setup.sh"

${SCRIPTS}/clean.sh

go mod tidy
go fmt ./...
go vet ./...

Expand Down
4 changes: 0 additions & 4 deletions internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ func (c *Client) Create(ctx context.Context, obj client.Object) error {
return c.Client.Create(ctx, obj, opts...)
}

func (c *Client) Upsert(ctx context.Context, obj client.Object) error {
return c.Client.Upsert(ctx, obj, c.cfg.Flags.DryRun)
}

func (c *Client) Apply(ctx context.Context, obj client.Object) error {
opts := []client.PatchOption{}
if c.cfg.Flags.DryRun {
Expand Down
3 changes: 3 additions & 0 deletions internal/repo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type DockerfileTar struct {
read int
}

// TODO switch to Buildah
// https://github.com/containers/buildah/blob/e089136922680583a37e40d97e86818b09be4875/imagebuildah/build.go#L66

func (r *repo) Build(compDirName string) string {
img := r.GetCompImageFromDir(compDirName)
appYaml := r.AppYAMLBuildSubpath()
Expand Down
86 changes: 43 additions & 43 deletions internal/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"github.com/xigxog/kubefox/api/kubernetes/v1alpha1"
"github.com/xigxog/kubefox/core"
"github.com/xigxog/kubefox/k8s"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func (r *repo) Release(appDepId string) *v1alpha1.Release {
func (r *repo) Release(appDepId string) *v1alpha1.VirtualEnv {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

Expand All @@ -26,48 +26,53 @@ func (r *repo) Release(appDepId string) *v1alpha1.Release {
log.Fatal("Error finding AppDeployment: %v", err)
}

envObj, err := r.k8s.SnapshotVirtualEnv(ctx, platform.Namespace, r.cfg.Flags.VirtEnv)
if err != nil {
log.Fatal("Error finding VirtualEnvironment: %v", err)
var envSnapName string
envSnap := &v1alpha1.VirtualEnvSnapshot{}
err = r.k8s.Get(ctx, k8s.Key(platform.Namespace, r.cfg.Flags.VirtEnv), envSnap)
if k8s.IgnoreNotFound(err) != nil {
log.Fatal("Error getting VirtualEnvSnapshot: %v", err)
}

var envSnapshotName string
if r.cfg.Flags.CreateVirtEnv {
if name, err := r.createEnvSnapshot(ctx, envObj); err != nil {
log.Fatal("Error creating VirtualEnvironmentSnapshot: %v", err)
} else {
envSnapshotName = name
if k8s.IsNotFound(err) {
if envSnap, err = r.k8s.SnapshotVirtualEnv(ctx, platform.Namespace, r.cfg.Flags.VirtEnv); err != nil {
log.Fatal("Error getting VirtualEnv: %v", err)
}
} else {
envSnapName = envSnap.Name
}

} else if envObj.Data.Source.Kind == "VirtualEnvSnapshot" {
envSnapshotName = envObj.Name
if envSnapName == "" && r.cfg.Flags.CreateVirtEnv {
if envSnapName, err = r.createEnvSnapshot(ctx, envSnap); err != nil {
log.Fatal("Error creating VirtualEnvSnapshot: %v", err)
}
}

rel := &v1alpha1.Release{
TypeMeta: metav1.TypeMeta{
env := &v1alpha1.VirtualEnv{
TypeMeta: v1.TypeMeta{
APIVersion: v1alpha1.GroupVersion.Identifier(),
Kind: "Release",
Kind: "VirtualEnv",
},
ObjectMeta: metav1.ObjectMeta{
Name: envObj.GetEnvName(),
ObjectMeta: v1.ObjectMeta{
Name: envSnap.Spec.Source.Name,
Namespace: platform.Namespace,
},
Spec: v1alpha1.ReleaseSpec{
VirtualEnvSnapshot: envSnapshotName,
AppDeployment: v1alpha1.ReleaseAppDeployment{
Name: appDep.Name,
Version: appDep.Spec.Version,
Spec: v1alpha1.VirtualEnvSpec{
Release: &v1alpha1.Release{
AppDeployment: v1alpha1.ReleaseAppDeployment{
Name: appDep.Name,
Version: appDep.Spec.Version,
},
VirtualEnvSnapshot: envSnapName,
},
},
}

if err := r.k8s.Apply(ctx, rel); err != nil {
log.Fatal("Error creating release: %v", err)
if err := r.k8s.Apply(ctx, env); err != nil {
log.Fatal("Error updating VirtualEnv with Release: %v", err)
}

r.waitForReady(platform, &appDep.Spec)

return rel
return env
}

func (r *repo) findAppDep(ctx context.Context, platform *v1alpha1.Platform, appDepId string) (*v1alpha1.AppDeployment, error) {
Expand Down Expand Up @@ -131,34 +136,29 @@ func (r *repo) pickAppDep(appDepList *v1alpha1.AppDeploymentList) *v1alpha1.AppD
return selected
}

func (r *repo) createEnvSnapshot(ctx context.Context, envObj *v1alpha1.VirtualEnvSnapshot) (string, error) {
if envObj.Data.Source.Kind == "VirtualEnvSnapshot" {
return "", fmt.Errorf("cannot create snapshot of existing VirtualEnvSnapshot")
}

log.Verbose("checking for existing snapshot of '%s' with resource version '%s'",
envObj.Data.Source.Name, envObj.Data.Source.ResourceVersion)
func (r *repo) createEnvSnapshot(ctx context.Context, env *v1alpha1.VirtualEnvSnapshot) (string, error) {
log.Verbose("checking for existing VirtualEnvSnapshot of VirtualEnv '%s' with resourceVersion '%s'",
env.Spec.Source.Name, env.Spec.Source.ResourceVersion)

list := &v1alpha1.VirtualEnvSnapshotList{}
if err := r.k8s.List(ctx, list, client.MatchingLabels{
api.LabelK8sVirtualEnv: envObj.Data.Source.Name,
api.LabelK8sSourceKind: envObj.Data.Source.Kind,
api.LabelK8sSourceResourceVersion: envObj.Data.Source.ResourceVersion,
api.LabelK8sVirtualEnv: env.Name,
api.LabelK8sSourceResourceVersion: env.ResourceVersion,
}); err != nil {
return "", err
}
for _, snap := range list.Items {
for _, s := range list.Items {
// Double check source is equal.
if snap.Data.Source == envObj.Data.Source {
if s.Spec.Source == env.Spec.Source {
log.Verbose("found existing snapshot '%s'", list.Items[0].Name)
return snap.Name, nil
return s.Name, nil
}
}

log.VerboseMarshal(envObj, "creating VirtualEnvSnapshot")
if err := r.k8s.Create(ctx, envObj); err != nil {
log.VerboseMarshal(env, "creating VirtualEnvSnapshot")
if err := r.k8s.Create(ctx, env); err != nil {
return "", err
}

return envObj.Name, nil
return env.Name, nil
}

0 comments on commit 1e6611b

Please sign in to comment.