Skip to content

Commit

Permalink
updated to support multi app release
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Jan 13, 2024
1 parent 9c0e268 commit 9bcaa62
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 92 deletions.
6 changes: 2 additions & 4 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,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 DataSnapshot if needed.
fox release v1.2.3 --virtual-env prod --create-snapshot
# VirtualEnvironment.
fox release v1.2.3 --virtual-env prod
`),
}

func init() {
releaseCmd.Flags().StringVarP(&cfg.Flags.VirtEnv, "virtual-env", "e", "", "name of VirtualEnvironment to use for Release")
releaseCmd.Flags().StringVarP(&cfg.Flags.Snapshot, "snapshot", "d", "", "name of DataSnapshot to use for Release")
releaseCmd.Flags().BoolVarP(&cfg.Flags.CreateSnapshot, "create-snapshot", "c", false, "create an immutable snapshot of VirtualEnvironment data and use for Release")

addCommonDeployFlags(releaseCmd)

Expand Down
6 changes: 2 additions & 4 deletions docs/fox_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ 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 DataSnapshot if needed.
fox release v1.2.3 --virtual-env prod --create-snapshot
# VirtualEnvironment.
fox release v1.2.3 --virtual-env prod
```

### Options

```
-c, --create-snapshot create an immutable snapshot of VirtualEnvironment data and use for Release
--dry-run submit server-side request without persisting the resource
-h, --help help for release
-n, --namespace string namespace of KubeFox Platform
-p, --platform string name of KubeFox Platform to utilize
-d, --snapshot string name of DataSnapshot to use for Release
-e, --virtual-env string name of VirtualEnvironment to use for Release
--wait duration wait up the specified time for components to be ready
```
Expand Down
1 change: 0 additions & 1 deletion efs/hello-world/hack/environments/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
releasePolicy:
versionRequired: true
dataSnapshotRequired: true
data:
vars:
subPath: prod
Expand Down
4 changes: 3 additions & 1 deletion efs/hello-world/hack/environments/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
releasePolicy:
versionRequired: false
dataSnapshotRequired: false
data:
vars:
who: World
Expand All @@ -17,3 +16,6 @@ metadata:
name: qa
spec:
environment: qa
data:
secrets:
this: thisisatest
14 changes: 6 additions & 8 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ type Flags struct {
Platform string
Version string
VirtEnv string
Snapshot string

CreateSnapshot bool
CreateTag bool
ForceBuild bool
NoCache bool
PushImage bool
Quickstart bool
SkipDeploy bool
CreateTag bool
ForceBuild bool
NoCache bool
PushImage bool
Quickstart bool
SkipDeploy bool

WaitTime time.Duration
}
89 changes: 15 additions & 74 deletions internal/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/xigxog/kubefox/api/kubernetes/v1alpha1"
"github.com/xigxog/kubefox/core"
"github.com/xigxog/kubefox/k8s"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -39,15 +38,13 @@ func (r *repo) Release(appDepId string) *v1alpha1.VirtualEnvironment {
if err := r.k8s.Get(ctx, k8s.Key(platform.Namespace, r.cfg.Flags.VirtEnv), ve); err != nil {
log.Fatal("Error getting VirtualEnvironment: %v", err)
}
origVE := ve.DeepCopy()

env := &v1alpha1.Environment{}
if err := r.k8s.Get(ctx, k8s.Key("", ve.Spec.Environment), env); err != nil {
log.Fatal("Error getting Environment: %v", err)
}
ve.Merge(env)

problems, err := appDep.Validate(&ve.Data, func(name string, typ api.ComponentType) (api.Adapter, error) {
problems, err := appDep.Validate(ve.Data.MergeInto(&env.Data), func(name string, typ api.ComponentType) (api.Adapter, error) {

Check failure on line 47 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Build binary

ve.Data.MergeInto undefined (type "github.com/xigxog/kubefox/api".Data has no field or method MergeInto)

Check failure on line 47 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

ve.Data.MergeInto undefined (type "github.com/xigxog/kubefox/api".Data has no field or method MergeInto)
switch typ {
case api.ComponentTypeHTTPAdapter:
a := &v1alpha1.HTTPAdapter{}
Expand All @@ -70,33 +67,25 @@ func (r *repo) Release(appDepId string) *v1alpha1.VirtualEnvironment {
}
}

snapName := r.cfg.Flags.Snapshot
if snapName == "" && r.cfg.Flags.CreateSnapshot {
if snapName, err = r.createDataSnapshot(ctx, ve); err != nil {
log.Fatal("Error creating DataSnapshot: %v", err)
}
} else if snapName != "" {
snap := &v1alpha1.DataSnapshot{}
if err := r.k8s.Get(ctx, k8s.Key(platform.Namespace, snapName), snap); err != nil {
log.Fatal("Error getting DataSnapshot: %v", err)
}
origVE := ve.DeepCopy()
if ve.Spec.Release == nil {
ve.Spec.Release = &v1alpha1.Release{}
}

updatedVE := origVE.DeepCopy()
updatedVE.Spec.Release = &v1alpha1.Release{
AppDeployment: v1alpha1.ReleaseAppDeployment{
Name: appDep.Name,
Version: appDep.Spec.Version,
},
DataSnapshot: snapName,
if ve.Spec.Release.Apps == nil {

Check failure on line 74 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Build binary

ve.Spec.Release.Apps undefined (type *"github.com/xigxog/kubefox/api/kubernetes/v1alpha1".Release has no field or method Apps)

Check failure on line 74 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

ve.Spec.Release.Apps undefined (type *"github.com/xigxog/kubefox/api/kubernetes/v1alpha1".Release has no field or method Apps)
ve.Spec.Release.Apps = map[string]v1alpha1.ReleaseApp{}

Check failure on line 75 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Build binary

ve.Spec.Release.Apps undefined (type *"github.com/xigxog/kubefox/api/kubernetes/v1alpha1".Release has no field or method Apps)

Check failure on line 75 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Build binary

undefined: v1alpha1.ReleaseApp

Check failure on line 75 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

ve.Spec.Release.Apps undefined (type *"github.com/xigxog/kubefox/api/kubernetes/v1alpha1".Release has no field or method Apps)

Check failure on line 75 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: v1alpha1.ReleaseApp
}
if err := r.k8s.Merge(ctx, updatedVE, origVE); err != nil {
ve.Spec.Release.Apps[appDep.Spec.AppName] = v1alpha1.ReleaseApp{

Check failure on line 77 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Build binary

ve.Spec.Release.Apps undefined (type *"github.com/xigxog/kubefox/api/kubernetes/v1alpha1".Release has no field or method Apps)

Check failure on line 77 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Build binary

undefined: v1alpha1.ReleaseApp

Check failure on line 77 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

ve.Spec.Release.Apps undefined (type *"github.com/xigxog/kubefox/api/kubernetes/v1alpha1".Release has no field or method Apps)

Check failure on line 77 in internal/repo/release.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: v1alpha1.ReleaseApp
AppDeployment: appDep.Name,
Version: appDep.Spec.Version,
}

if err := r.k8s.Merge(ctx, ve, origVE); err != nil {
log.Fatal("Error updating Release: %v", err)
}

r.waitForReady(platform, &appDep.Spec)

return updatedVE
return ve
}

func (r *repo) findAppDep(ctx context.Context, platform *v1alpha1.Platform, appDepId string) (*v1alpha1.AppDeployment, error) {
Expand All @@ -120,7 +109,8 @@ func (r *repo) findAppDep(ctx context.Context, platform *v1alpha1.Platform, appD
for _, label := range labels {
appDepList := &v1alpha1.AppDeploymentList{}
if err := r.k8s.List(ctx, appDepList, client.MatchingLabels{
label: appDepId,
api.LabelK8sAppName: r.app.Name,
label: appDepId,
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,52 +149,3 @@ func (r *repo) pickAppDep(appDepList *v1alpha1.AppDeploymentList) *v1alpha1.AppD

return selected
}

func (r *repo) createDataSnapshot(ctx context.Context, ve *v1alpha1.VirtualEnvironment) (string, error) {
log.Verbose("checking for existing DataSnapshot of VirtualEnvironment '%s' with resourceVersion '%s'",
ve.Name, ve.ResourceVersion)

list := &v1alpha1.DataSnapshotList{}
if err := r.k8s.List(ctx, list, client.MatchingLabels{
api.LabelK8sSourceKind: string(api.DataSourceKindVirtualEnvironment),
api.LabelK8sSourceName: ve.Name,
api.LabelK8sSourceVersion: ve.ResourceVersion,
}); err != nil {
return "", err
}
dataSource := v1alpha1.DataSource{
Kind: api.DataSourceKindVirtualEnvironment,
Name: ve.Name,
ResourceVersion: ve.ResourceVersion,
DataChecksum: ve.GetDataChecksum(),
}

for _, s := range list.Items {
// Double check source is equal.
if s.Spec.Source == dataSource {
log.Verbose("found existing snapshot '%s'", s.Name)
return s.Name, nil
}
}

log.VerboseMarshal(ve, "creating DataSnapshot")
dataSnap := &v1alpha1.DataSnapshot{
TypeMeta: v1.TypeMeta{
APIVersion: v1alpha1.GroupVersion.Identifier(),
Kind: "DataSnapshot",
},
ObjectMeta: v1.ObjectMeta{
Namespace: ve.Namespace,
Name: fmt.Sprintf("%s-%s-%s",
ve.Name, ve.ResourceVersion, time.Now().UTC().Format("20060102-150405")),
},
Spec: v1alpha1.DataSnapshotSpec{
Source: dataSource,
},
}
if err := r.k8s.Create(ctx, dataSnap); err != nil {
return "", err
}

return dataSnap.Name, nil
}

0 comments on commit 9bcaa62

Please sign in to comment.