Skip to content

Commit

Permalink
"okteto stack deploy" and "okteto up" building okteto registry images…
Browse files Browse the repository at this point in the history
… if they don't exist (#1154)

* Okteto up building dev image if it does not exists

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Okteto stack deploy building dev image if it doesn't exist

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Address review comments

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>
  • Loading branch information
pchico83 committed Nov 6, 2020
1 parent 6feec2d commit 3133890
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 210 deletions.
1 change: 1 addition & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func Build(ctx context.Context) *cobra.Command {
if err != nil {
return err
}
log.Information("Running your build in %s...", buildKitHost)

ctx := context.Background()
if _, err := build.Run(ctx, buildKitHost, isOktetoCluster, path, file, tag, target, noCache, cacheFrom, buildArgs, progress); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/model"
"github.com/okteto/okteto/pkg/okteto"
"github.com/okteto/okteto/pkg/registry"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -218,11 +219,12 @@ func buildImage(ctx context.Context, dev *model.Dev, imageTag, imageFromDeployme
if err != nil {
return "", err
}
log.Information("Running your build in %s...", buildKitHost)

if imageTag == "" {
imageTag = dev.Push.Name
}
buildTag := build.GetDevImageTag(dev, imageTag, imageFromDeployment, oktetoRegistryURL)
buildTag := registry.GetDevImageTag(dev, imageTag, imageFromDeployment, oktetoRegistryURL)
log.Infof("pushing with image tag %s", buildTag)

var imageDigest string
Expand All @@ -233,7 +235,7 @@ func buildImage(ctx context.Context, dev *model.Dev, imageTag, imageFromDeployme
}

if imageDigest != "" {
imageWithoutTag := build.GetRepoNameWithoutTag(buildTag)
imageWithoutTag, _ := registry.GetRepoNameAndTag(buildTag)
buildTag = fmt.Sprintf("%s@%s", imageWithoutTag, imageDigest)
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/model"
"github.com/okteto/okteto/pkg/okteto"
"github.com/okteto/okteto/pkg/registry"
"github.com/okteto/okteto/pkg/ssh"

"github.com/okteto/okteto/pkg/k8s/forward"
Expand Down Expand Up @@ -328,6 +329,11 @@ func (up *upContext) activate(isRetry, autoDeploy, build bool) error {
}
}

if _, err := registry.GetImageTagWithDigest(ctx, up.Dev.Image.Name); err == errors.ErrNotFound {
log.Infof("image '%s' not found, building it: %s", up.Dev.Image.Name, err.Error())
build = true
}

if !isRetry && build {
if err := up.buildDevImage(ctx, d, create); err != nil {
return fmt.Errorf("error building dev image: %s", err)
Expand Down Expand Up @@ -494,8 +500,9 @@ func (up *upContext) buildDevImage(ctx context.Context, d *appsv1.Deployment, cr
if err != nil {
return err
}
log.Information("Running your build in %s...", buildKitHost)

imageTag := buildCMD.GetImageTag(up.Dev.Image.Name, up.Dev.Name, up.Dev.Namespace, oktetoRegistryURL)
imageTag := registry.GetImageTag(up.Dev.Image.Name, up.Dev.Name, up.Dev.Namespace, oktetoRegistryURL)
log.Infof("building dev image tag %s", imageTag)

var imageDigest string
Expand All @@ -505,7 +512,7 @@ func (up *upContext) buildDevImage(ctx context.Context, d *appsv1.Deployment, cr
return fmt.Errorf("error building dev image '%s': %s", imageTag, err)
}
if imageDigest != "" {
imageWithoutTag := buildCMD.GetRepoNameWithoutTag(imageTag)
imageWithoutTag, _ := registry.GetRepoNameAndTag(imageTag)
imageTag = fmt.Sprintf("%s@%s", imageWithoutTag, imageDigest)
}
for _, s := range up.Dev.Services {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/google/uuid v1.1.2
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/hashicorp/go-getter v1.5.0
github.com/heroku/docker-registry-client v0.0.0-20190909225348-afc9e1acc3d5
github.com/kr/pretty v0.2.0 // indirect
github.com/machinebox/graphql v0.2.2
github.com/manifoldco/promptui v0.3.2
Expand Down
92 changes: 92 additions & 0 deletions go.sum

Large diffs are not rendered by default.

41 changes: 4 additions & 37 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ package build

import (
"context"
"fmt"
"os"
"strings"

"github.com/okteto/okteto/pkg/k8s/client"
"github.com/okteto/okteto/pkg/k8s/namespaces"
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/okteto"
"github.com/okteto/okteto/pkg/registry"
"github.com/pkg/errors"
)

Expand All @@ -34,7 +30,7 @@ func Run(ctx context.Context, buildKitHost string, isOktetoCluster bool, path, d
return "", err
}

processedDockerfile, err := getDockerFile(path, dockerFile, isOktetoCluster)
processedDockerfile, err := registry.GetDockerfile(path, dockerFile, isOktetoCluster)
if err != nil {
return "", err
}
Expand All @@ -43,12 +39,12 @@ func Run(ctx context.Context, buildKitHost string, isOktetoCluster bool, path, d
defer os.Remove(processedDockerfile)
}

tag, err = expandOktetoDevRegistry(ctx, tag)
tag, err = registry.ExpandOktetoDevRegistry(ctx, tag)
if err != nil {
return "", err
}
for i := range cacheFrom {
cacheFrom[i], err = expandOktetoDevRegistry(ctx, cacheFrom[i])
cacheFrom[i], err = registry.ExpandOktetoDevRegistry(ctx, cacheFrom[i])
if err != nil {
return "", err
}
Expand All @@ -60,32 +56,3 @@ func Run(ctx context.Context, buildKitHost string, isOktetoCluster bool, path, d

return solveBuild(ctx, buildkitClient, opt, progress)
}

func expandOktetoDevRegistry(ctx context.Context, tag string) (string, error) {
if !strings.HasPrefix(tag, okteto.DevRegistry) {
return tag, nil
}

c, _, namespace, err := client.GetLocal("")
if err != nil {
return "", fmt.Errorf("failed to load your local Kubeconfig: %s", err)
}
n, err := namespaces.Get(ctx, namespace, c)
if err != nil {
return "", fmt.Errorf("failed to get your current namespace '%s': %s", namespace, err.Error())
}
if !namespaces.IsOktetoNamespace(n) {
return "", fmt.Errorf("cannot use the okteto.dev container registry: your current namespace '%s' is not managed by okteto", namespace)
}

oktetoRegistryURL, err := okteto.GetRegistry()
if err != nil {
return "", fmt.Errorf("cannot use the okteto.dev container registry: unable to get okteto registry url: %s", err)
}

oldTag := tag
tag = strings.Replace(tag, okteto.DevRegistry, fmt.Sprintf("%s/%s", oktetoRegistryURL, namespace), 1)

log.Information("'%s' expanded to '%s'.", oldTag, tag)
return tag, nil
}
25 changes: 1 addition & 24 deletions pkg/cmd/build/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,13 @@ const (
func GetBuildKitHost() (string, bool, error) {
buildKitHost := os.Getenv("BUILDKIT_HOST")
if buildKitHost != "" {
log.Information("Running your build in %s...", buildKitHost)
return buildKitHost, false, nil
}
buildkitURL, err := okteto.GetBuildKit()
if err != nil {
return "", false, err
}
if buildkitURL == okteto.CloudBuildKitURL {
log.Information("Running your build in Okteto Cloud...")
} else {
log.Information("Running your build in Okteto Enterprise...")
}
return buildkitURL, true, err
return buildkitURL, true, nil
}

//getSolveOpt returns the buildkit solve options
Expand Down Expand Up @@ -135,23 +129,6 @@ func getSolveOpt(buildCtx, file, imageTag, target string, noCache bool, cacheFro
return opt, nil
}

func getDockerFile(path, dockerFile string, isOktetoCluster bool) (string, error) {
if dockerFile == "" {
dockerFile = filepath.Join(path, "Dockerfile")
}

if !isOktetoCluster {
return dockerFile, nil
}

fileWithCacheHandler, err := getDockerfileWithCacheHandler(dockerFile)
if err != nil {
return "", errors.Wrap(err, "failed to create temporary build folder")
}

return fileWithCacheHandler, nil
}

func getBuildkitClient(ctx context.Context, isOktetoCluster bool, buildKitHost string) (*client.Client, error) {
if isOktetoCluster {
c, err := getClientForOktetoCluster(ctx, buildKitHost)
Expand Down
59 changes: 0 additions & 59 deletions pkg/cmd/build/buildkit_test.go

This file was deleted.

31 changes: 18 additions & 13 deletions pkg/cmd/stack/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"strings"

"github.com/okteto/okteto/pkg/cmd/build"
"github.com/okteto/okteto/pkg/errors"
k8Client "github.com/okteto/okteto/pkg/k8s/client"
"github.com/okteto/okteto/pkg/k8s/namespaces"
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/model"
"github.com/okteto/okteto/pkg/okteto"
"github.com/okteto/okteto/pkg/registry"
"github.com/subosito/gotenv"
)

Expand All @@ -42,10 +44,8 @@ func translate(ctx context.Context, s *model.Stack, forceBuild, noCache bool) er
return nil
}

if forceBuild {
if err := translateBuildImages(ctx, s, noCache); err != nil {
return err
}
if err := translateBuildImages(ctx, s, forceBuild, noCache); err != nil {
return err
}
return nil
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func translateEnvFile(svc *model.Service, filename string) error {
return nil
}

func translateBuildImages(ctx context.Context, s *model.Stack, noCache bool) error {
func translateBuildImages(ctx context.Context, s *model.Stack, forceBuild, noCache bool) error {
c, _, configNamespace, err := k8Client.GetLocal("")
if err != nil {
return err
Expand All @@ -126,14 +126,23 @@ func translateBuildImages(ctx context.Context, s *model.Stack, noCache bool) err
if err != nil {
return err
}
building := false

oneBuild := false
for name, svc := range s.Services {
if svc.Build == nil {
continue
}
oneBuild = true
imageTag := build.GetImageTag(svc.Image, name, s.Namespace, oktetoRegistryURL)
if !forceBuild {
if _, err := registry.GetImageTagWithDigest(ctx, svc.Image); err != errors.ErrNotFound {
continue
}
log.Infof("image '%s' not found, building it", svc.Image)
}
if !building {
building = true
log.Information("Running your build in %s...", buildKitHost)
}
imageTag := registry.GetImageTag(svc.Image, name, s.Namespace, oktetoRegistryURL)
log.Information("Building image for service '%s'...", name)
var imageDigest string
buildArgs := model.SerializeBuildArgs(svc.Build.Args)
Expand All @@ -142,17 +151,13 @@ func translateBuildImages(ctx context.Context, s *model.Stack, noCache bool) err
return fmt.Errorf("error building image for '%s': %s", name, err)
}
if imageDigest != "" {
imageWithoutTag := build.GetRepoNameWithoutTag(imageTag)
imageWithoutTag, _ := registry.GetRepoNameAndTag(imageTag)
imageTag = fmt.Sprintf("%s@%s", imageWithoutTag, imageDigest)
}
svc.Image = imageTag
s.Services[name] = svc
log.Success("Image for service '%s' successfully pushed", name)
}

if !oneBuild {
log.Information("No build directives found in your Stack manifest")
}

return nil
}
43 changes: 43 additions & 0 deletions pkg/registry/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 The Okteto Authors
// 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 registry

import (
"net/http"
"strings"

"github.com/heroku/docker-registry-client/registry"
"github.com/okteto/okteto/pkg/log"
)

// NewRegistryClient creates a new Registry with the given URL and credentials, then Ping()s it
// before returning it to verify that the registry is available.
func NewRegistryClient(registryURL, username, password string) (*registry.Registry, error) {
transport := http.DefaultTransport
return newFromTransport(registryURL, username, password, transport)
}

func newFromTransport(registryURL, username, password string, transport http.RoundTripper) (*registry.Registry, error) {
url := strings.TrimSuffix(registryURL, "/")
transport = registry.WrapTransport(transport, url, username, password)
registry := &registry.Registry{
URL: url,
Client: &http.Client{
Transport: transport,
},
Logf: log.Infof,
}

return registry, nil
}

0 comments on commit 3133890

Please sign in to comment.