Skip to content

Commit

Permalink
update hello-world to match example repo, small output cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Oct 24, 2023
1 parent df6d986 commit 44f051c
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
}

func addCommonBuildFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&cfg.Flags.Kind, "kind", "k", "", "if provided the built image will be loaded into the Kind cluster")
cmd.Flags().StringVarP(&cfg.Flags.Kind, "kind", "k", "", "if provided the built image will be loaded into the kind cluster")
cmd.Flags().BoolVarP(&cfg.Flags.NoCache, "no-cache", "", false, "do not use cache when building image")
cmd.Flags().BoolVarP(&cfg.Flags.ForceBuild, "force", "", false, "force build even if component image exists")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func runDeploy(cmd *cobra.Command, args []string) {
checkCommonDeployFlags(name)

r := repo.New(cfg)
d := r.Deploy(name)
d := r.Deploy(name, false)
// Makes output less cluttered.
d.ManagedFields = nil
log.Marshal(d)
Expand Down
2 changes: 1 addition & 1 deletion docs/fox_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fox build (component name) [flags]
```
--force force build even if component image exists
-h, --help help for build
-k, --kind string if provided the built image will be loaded into the Kind cluster
-k, --kind string if provided the built image will be loaded into the kind cluster
--no-cache do not use cache when building image
--push publish image to OCI image registry
```
Expand Down
2 changes: 1 addition & 1 deletion docs/fox_publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fox publish (deploy-name) [flags]
```
--force force build even if component image exists
-h, --help help for publish
-k, --kind string if provided the built image will be loaded into the Kind cluster
-k, --kind string if provided the built image will be loaded into the kind cluster
-n, --namespace string namespace of platform
--no-cache do not use cache when building image
-p, --platform string name of platform to utilize
Expand Down
2 changes: 1 addition & 1 deletion docs/fox_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fox release (release name) [flags]
--env-version string environment resource version to release to
--force force build even if component image exists
-h, --help help for release
-k, --kind string if provided the built image will be loaded into the Kind cluster
-k, --kind string if provided the built image will be loaded into the kind cluster
-n, --namespace string namespace of platform
--no-cache do not use cache when building image
-p, --platform string name of platform to utilize
Expand Down
6 changes: 3 additions & 3 deletions efs/hello-world/components/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import "github.com/xigxog/kubefox/libs/core/kit"

func main() {
k := kit.New()
k.Default(sayHello)
k.Default(sayWho)
k.Start()
}

func sayHello(k kit.Kontext) error {
who := k.EnvDef("who", "World")
func sayWho(k kit.Kontext) error {
who := k.EnvDef("HELLO_WORLD_WHO", "World")
k.Log().Infof("The who is '%s'!", who)

return k.Resp().SendStr(who)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ metadata:
name: universe
spec:
vars:
who: Universe
HELLO_WORLD_WHO: Universe
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ metadata:
name: world
spec:
vars:
who: World
HELLO_WORLD_WHO: World
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ func (cfg *Config) Setup() {
log.Info("Please make sure your workstation has Docker installed (https://docs.docker.com/engine/install)")
log.Info("and that KubeFox is installed (https://docs.kubefox.io/install) on your Kubernetes cluster.")
log.InfoNewline()
log.Info("If you don't have a Kubernetes cluster you can run one locally with Kind (https://kind.sigs.k8s.io)")
log.Info("If you don't have a Kubernetes cluster you can run one locally with kind (https://kind.sigs.k8s.io)")
log.Info("to experiment with KubeFox.")
log.InfoNewline()
log.Info("🦊 Fox needs a place to store the component images it will build, normally this is")
log.Info("a remote container registry. However, if you only want to use KubeFox locally")
log.Info("with Kind you can skip this step.")
kindOnly := utils.YesNoPrompt("Are you only using KubeFox with local Kind cluster?", false)
log.Info("with kind you can skip this step.")
kindOnly := utils.YesNoPrompt("Are you only using KubeFox with local kind cluster?", false)
if kindOnly {
cfg.ContainerRegistry.Address = LocalRegistry
cfg.ContainerRegistry.Token = ""
cfg.Kind.ClusterName = utils.NamePrompt("Kind cluster", "kind", true)
cfg.Kind.ClusterName = utils.NamePrompt("kind cluster", "kind", true)
cfg.Kind.AlwaysLoad = true
log.InfoNewline()
cfg.done()
Expand Down
7 changes: 4 additions & 3 deletions internal/repo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (r *repo) Build(compDirName string) string {
log.Fatal("Error creating container tar: %v", err)
}
labels := map[string]string{
"com.xigxog.kubefox.app": r.app.Name, // TODO replace with const from lib when released
kubefox.LabelOCIComponent: compName,
kubefox.LabelOCICreated: time.Now().Format(time.RFC3339),
kubefox.LabelOCIRevision: gitCommit,
Expand Down Expand Up @@ -168,18 +169,18 @@ func (r *repo) KindLoad(img string) {
return
}

log.Info("Loading component image '%s' into Kind cluster '%s'.", img, kind)
log.Info("Loading component image '%s' into kind cluster '%s'.", img, kind)
if found, err := r.DoesImageExists(img, true); !found {
if err != nil {
log.Fatal("Error loading component image into Kind: %v", err)
log.Fatal("Error loading component image into kind: %v", err)
}
log.Fatal("Component image does not exist, please build it first.")
}

cmd := exec.Command("kind", "load", "docker-image", "--name="+kind, img)
if out, err := cmd.CombinedOutput(); err != nil {
log.Error("%s", strings.TrimSpace(string(out)))
log.Fatal("Error loading component image into Kind: %v", err)
log.Fatal("Error loading component image into kind: %v", err)
} else {
log.Verbose("%s", strings.TrimSpace(string(out)))
}
Expand Down
50 changes: 26 additions & 24 deletions internal/repo/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *repo) Deploy(name string) *v1alpha1.Deployment {
p, spec := r.prepareDeployment()
func (r *repo) Deploy(name string, skipImageCheck bool) *v1alpha1.Deployment {
p, spec := r.prepareDeployment(skipImageCheck)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down Expand Up @@ -56,7 +56,7 @@ func (r *repo) Publish(deployName string) *v1alpha1.Deployment {
}

if !r.cfg.Flags.SkipDeploy && deployName != "" {
return r.Deploy(deployName)
return r.Deploy(deployName, true)
}
return nil
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (r *repo) applyIPS(ctx context.Context, p *v1alpha1.Platform, spec *v1alpha
// prepareDeployment pulls the Platform, generates the DeploymentSpec and
// ensures all images exist. If there are any issues it will prompt the user to
// correct them.
func (r *repo) prepareDeployment() (*v1alpha1.Platform, *v1alpha1.DeploymentSpec) {
func (r *repo) prepareDeployment(skipImageCheck bool) (*v1alpha1.Platform, *v1alpha1.DeploymentSpec) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

Expand All @@ -103,29 +103,31 @@ func (r *repo) prepareDeployment() (*v1alpha1.Platform, *v1alpha1.DeploymentSpec
log.Fatal("Error getting platform :%v", err)
}

allFound := true
for n, c := range spec.Components {
img := r.GetCompImage(n, c.Commit)
if found, _ := r.DoesImageExists(img, false); found {
log.Info("Component image '%s' exists.", img)
if r.cfg.IsRegistryLocal() {
r.KindLoad(img)
if !skipImageCheck {
allFound := true
for n, c := range spec.Components {
img := r.GetCompImage(n, c.Commit)
if found, _ := r.DoesImageExists(img, false); found {
log.Info("Component image '%s' exists.", img)
if r.cfg.IsRegistryLocal() {
r.KindLoad(img)
}
} else {
log.Warn("Component image '%s' does not exist.", img)
allFound = false
}
} else {
log.Warn("Component image '%s' does not exist.", img)
allFound = false
log.InfoNewline()
}
log.InfoNewline()
}

if !allFound {
log.Info("There are one or more missing component images. 🦊 Fox will need to build and")
log.Info("push them to the container registry before continuing with the operation.")
if utils.YesNoPrompt("Missing component images, would you like to publish them?", true) {
log.InfoNewline()
r.Publish("")
} else {
log.Fatal("There are one or more missing component images.")
if !allFound {
log.Info("There are one or more missing component images. 🦊 Fox will need to build and")
log.Info("push them to the container registry before continuing with the operation.")
if utils.YesNoPrompt("Missing component images, would you like to publish them?", true) {
log.InfoNewline()
r.Publish("")
} else {
log.Fatal("There are one or more missing component images.")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (r *repo) Release(name string) *v1alpha1.Release {
p, spec := r.prepareDeployment()
p, spec := r.prepareDeployment(false)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down
2 changes: 2 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func InputPrompt(prompt, def string, required bool) string {
log.Printf(" (default '%s')", def)
} else if required {
log.Printf(" (required)")
} else {
log.Printf(" (optional)")
}
log.Printf(": ")

Expand Down

0 comments on commit 44f051c

Please sign in to comment.