Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container issues w/ Jib - Release 2.1.x backport #5137

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions pkg/builder/jib.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util/jib"
"github.com/apache/camel-k/v2/pkg/util/log"
"github.com/apache/camel-k/v2/pkg/util/maven"
"github.com/apache/camel-k/v2/pkg/util/registry"
)

type jibTask struct {
Expand Down Expand Up @@ -84,18 +85,13 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus {

registryConfigDir := ""
if t.task.Registry.Secret != "" {
registryConfigDir, err = MountSecret(ctx, t.c, t.build.Namespace, t.task.Registry.Secret)
registryConfigDir, err = registry.MountSecretRegistryConfig(ctx, t.c, t.build.Namespace, "jib-secret-", t.task.Registry.Secret)
os.Setenv(jib.JibRegistryConfigEnvVar, registryConfigDir)
if err != nil {
return status.Failed(err)
}
}

if registryConfigDir != "" {
if err := os.RemoveAll(registryConfigDir); err != nil {
return status.Failed(err)
}
}

// TODO refactor maven code to avoid creating a file to pass command args
mavenCommand, err := util.ReadFile(filepath.Join(mavenDir, "MAVEN_CONTEXT"))
if err != nil {
Expand All @@ -120,8 +116,10 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus {
cmd.Dir = mavenDir

myerror := util.RunAndLog(ctx, cmd, maven.MavenLogHandler, maven.MavenLogHandler)

if myerror != nil {
log.Errorf(myerror, "jib integration image containerization did not run successfully")
_ = cleanRegistryConfig(registryConfigDir)
return status.Failed(myerror)
} else {
log.Debug("jib integration image containerization did run successfully")
Expand All @@ -130,10 +128,27 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus {
// retrieve image digest
mavenDigest, errDigest := util.ReadFile(filepath.Join(mavenDir, jib.JibDigestFile))
if errDigest != nil {
_ = cleanRegistryConfig(registryConfigDir)
return status.Failed(errDigest)
}
status.Digest = string(mavenDigest)
}

if registryConfigDir != "" {
if err := cleanRegistryConfig(registryConfigDir); err != nil {
return status.Failed(err)
}
}

return status
}

func cleanRegistryConfig(registryConfigDir string) error {
if err := os.Unsetenv(jib.JibRegistryConfigEnvVar); err != nil {
return err
}
if err := os.RemoveAll(registryConfigDir); err != nil {
return err
}
return nil
}
39 changes: 2 additions & 37 deletions pkg/builder/spectrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ import (
"runtime"
"strings"

"go.uber.org/multierr"

spectrum "github.com/container-tools/spectrum/pkg/builder"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/client"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/log"
"github.com/apache/camel-k/v2/pkg/util/registry"
)

type spectrumTask struct {
Expand Down Expand Up @@ -100,7 +97,7 @@ func (t *spectrumTask) Do(ctx context.Context) v1.BuildStatus {

registryConfigDir := ""
if t.task.Registry.Secret != "" {
registryConfigDir, err = MountSecret(ctx, t.c, t.build.Namespace, t.task.Registry.Secret)
registryConfigDir, err = registry.MountSecretRegistryConfig(ctx, t.c, t.build.Namespace, "spectrum-secret-", t.task.Registry.Secret)
if err != nil {
return status.Failed(err)
}
Expand Down Expand Up @@ -158,35 +155,3 @@ func readSpectrumLogs(newStdOut io.Reader) {
log.Infof(line)
}
}

func MountSecret(ctx context.Context, c client.Client, namespace, name string) (string, error) {
dir, err := os.MkdirTemp("", "spectrum-secret-")
if err != nil {
return "", err
}

secret, err := c.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if removeErr := os.RemoveAll(dir); removeErr != nil {
err = multierr.Append(err, removeErr)
}
return "", err
}

for file, content := range secret.Data {
if err := os.WriteFile(filepath.Join(dir, remap(file)), content, 0o600); err != nil {
if removeErr := os.RemoveAll(dir); removeErr != nil {
err = multierr.Append(err, removeErr)
}
return "", err
}
}
return dir, nil
}

func remap(name string) string {
if name == ".dockerconfigjson" {
return "config.json"
}
return name
}
3 changes: 3 additions & 0 deletions pkg/util/jib/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const JibDigestFile = "target/jib-image.digest"
const JibMavenPluginVersionDefault = "3.3.2"
const JibLayerFilterExtensionMavenVersionDefault = "0.3.0"

// See: https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/README.md#using-docker-configuration-files
const JibRegistryConfigEnvVar = "DOCKER_CONFIG"

type JibBuild struct {
Plugins []maven.Plugin `xml:"plugins>plugin,omitempty"`
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ limitations under the License.
package registry

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/apache/camel-k/v2/pkg/client"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var knownServersByRegistry = map[string]string{
Expand Down Expand Up @@ -98,3 +105,36 @@ func (a Auth) getActualServer() string {
func (a Auth) encodedCredentials() string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.Username, a.Password)))
}

// MountSecretRegistryConfig write a file containing the secret registry config in a temporary folder.
func MountSecretRegistryConfig(ctx context.Context, c client.Client, namespace, prefix, name string) (string, error) {
dir, err := os.MkdirTemp("", prefix)
if err != nil {
return "", err
}

secret, err := c.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if removeErr := os.RemoveAll(dir); removeErr != nil {
err = multierr.Append(err, removeErr)
}
return "", err
}

for file, content := range secret.Data {
if err := os.WriteFile(filepath.Join(dir, remap(file)), content, 0o600); err != nil {
if removeErr := os.RemoveAll(dir); removeErr != nil {
err = multierr.Append(err, removeErr)
}
return "", err
}
}
return dir, nil
}

func remap(name string) string {
if name == ".dockerconfigjson" {
return "config.json"
}
return name
}
41 changes: 41 additions & 0 deletions pkg/util/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ limitations under the License.
package registry

import (
"context"
"os"
"testing"

"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/test"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestAuth_GenerateDockerConfig(t *testing.T) {
Expand Down Expand Up @@ -66,3 +72,38 @@ func TestAuth_Validate(t *testing.T) {
Server: "quay.io",
}.validate())
}

func TestMountSecretRegistryConfig(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a := Auth{
Username: "nic",
Registry: "docker.io",
}
conf, _ := a.GenerateDockerConfig()
namespace := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
}
secret := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "my-secret1",
},
Type: v1.SecretTypeDockerConfigJson,
Data: map[string][]byte{
v1.DockerConfigJsonKey: conf,
},
}

c, err := test.NewFakeClient(&namespace, &secret)
assert.Nil(t, err)
assert.NotNil(t, c)
registryConfigDir, err := MountSecretRegistryConfig(ctx, c, "test", "prefix-", "my-secret1")
assert.Nil(t, err)
assert.NotNil(t, registryConfigDir)
dockerfileExists, _ := util.FileExists(registryConfigDir + "/config.json")
assert.True(t, dockerfileExists)
os.RemoveAll(registryConfigDir)
}
Loading