Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
Signed-off-by: jason yang <jasonyangshadow@gmail.com>
  • Loading branch information
JasonYangShadow committed Sep 6, 2024
1 parent 9fc70dd commit 4838e0c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
17 changes: 9 additions & 8 deletions cmd/internal/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,30 +475,31 @@ func checkBuildTarget(path string) error {
// performed. If `--docker-login` has not been specified, and explicit
// credentials have not been supplied via env-vars/flags, then a nil AuthConfig
// will be returned.
func makeOCICredentials(cmd *cobra.Command) (authConf *authn.AuthConfig, err error) {
func makeOCICredentials(cmd *cobra.Command) (*authn.AuthConfig, error) {
usernameFlag := cmd.Flags().Lookup("docker-username")
passwordFlag := cmd.Flags().Lookup("docker-password")

var err error
if dockerLogin {
if !usernameFlag.Changed {
authConf.Username, err = interactive.AskQuestion("Enter Docker Username: ")
authConfig.Username, err = interactive.AskQuestion("Enter Docker Username: ")
if err != nil {
return
return &authConfig, nil
}
usernameFlag.Value.Set(authConf.Username)
usernameFlag.Value.Set(authConfig.Username)
usernameFlag.Changed = true
}

authConf.Password, err = interactive.AskQuestionNoEcho("Enter Docker Password: ")
authConfig.Password, err = interactive.AskQuestionNoEcho("Enter Docker Password: ")
if err != nil {
return
return &authConfig, nil
}
passwordFlag.Value.Set(authConf.Password)
passwordFlag.Value.Set(authConfig.Password)
passwordFlag.Changed = true
}

if usernameFlag.Changed || passwordFlag.Changed {
return
return &authConfig, nil
}

// If a username / password have not been explicitly set, return a nil
Expand Down
7 changes: 3 additions & 4 deletions cmd/internal/cli/build_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/apptainer/apptainer/pkg/util/cryptkey"
"github.com/apptainer/apptainer/pkg/util/namespaces"
keyClient "github.com/apptainer/container-key-client/client"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -229,11 +228,11 @@ func runBuild(cmd *cobra.Command, args []string) {
sylog.Fatalf("While checking build target: %s", err)
}

runBuildLocal(cmd.Context(), &authConfig, cmd, dest, spec, fakerootPath)
runBuildLocal(cmd.Context(), cmd, dest, spec, fakerootPath)
sylog.Infof("Build complete: %s", dest)
}

func runBuildLocal(ctx context.Context, authConf *authn.AuthConfig, cmd *cobra.Command, dst, spec string, fakerootPath string) {
func runBuildLocal(ctx context.Context, cmd *cobra.Command, dst, spec string, fakerootPath string) {
var keyInfo *cryptkey.KeyInfo
unprivilege := false
if buildArgs.encrypt || promptForPassphrase || cmd.Flags().Lookup("pem-path").Changed {
Expand Down Expand Up @@ -270,7 +269,7 @@ func runBuildLocal(ctx context.Context, authConf *authn.AuthConfig, cmd *cobra.C
sylog.Fatalf("Could not check build sections: %v", err)
}

authConf, err = makeOCICredentials(cmd)
authConf, err := makeOCICredentials(cmd)
if err != nil {
sylog.Fatalf("While creating Docker credentials: %v", err)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/pkg/build/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func ConvertReference(ctx context.Context, imgCache *cache.Handle, src types.Ima
return nil, fmt.Errorf("undefined image cache")
}

if topts == nil {
// nolint:staticcheck
topts = ocitransport.TransportOptionsFromSystemContext(nil)
}

// Our cache dir is an OCI directory. We are using this as a 'blob pool'
// storing all incoming containers under unique tags, which are a hash of
// their source URI.
Expand Down Expand Up @@ -188,6 +193,7 @@ func getRefDigest(ctx context.Context, ref types.ImageReference, topts *ocitrans
}

// Otherwise get the manifest and calculate sha256 over it
// nolint:staticcheck
source, err := ref.NewImageSource(ctx, ocitransport.SystemContextFromTransportOptions(topts))
if err != nil {
return "", err
Expand All @@ -211,6 +217,7 @@ func getRefDigest(ctx context.Context, ref types.ImageReference, topts *ocitrans

// getDockerRefDigest obtains the manifest digest for a docker ref.
func getDockerRefDigest(ctx context.Context, ref types.ImageReference, topts *ocitransport.TransportOptions) (digest string, err error) {
// nolint:staticcheck
d, err := docker.GetDigest(ctx, ocitransport.SystemContextFromTransportOptions(topts), ref)
if err != nil {
return "", err
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/build/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func TestConvertReference(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// nolint: staticcheck
_, err := ConvertReference(context.Background(), imgCache, tt.ref, ocitransport.TransportOptionsFromSystemContext(tt.ctx))
if tt.shouldPass == true && err != nil {
t.Fatalf("test expected to succeeded but failed: %s\n", err)
Expand Down Expand Up @@ -345,6 +346,7 @@ func TestImageNameAndImageSHA(t *testing.T) {
for _, tt := range tests {
testName := "ParseImageName - " + tt.name
t.Run(testName, func(t *testing.T) {
// nolint:staticcheck
_, err := ParseImageName(context.Background(), imgCache, tt.uri, ocitransport.TransportOptionsFromSystemContext(tt.ctx))
if tt.shouldPass == true && err != nil {
t.Fatalf("test expected to succeeded but failed: %s\n", err)
Expand All @@ -356,6 +358,7 @@ func TestImageNameAndImageSHA(t *testing.T) {

testName = "ImageSHA - " + tt.name
t.Run(testName, func(t *testing.T) {
// nolint: staticcheck
_, err := ImageDigest(context.Background(), tt.uri, ocitransport.TransportOptionsFromSystemContext(tt.ctx))
if tt.shouldPass == true && err != nil {
t.Fatal("test expected to succeeded but failed")
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/build/sources/conveyorPacker_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,16 @@ func (cp *OCIConveyorPacker) Pack(ctx context.Context) (*sytypes.Bundle, error)
func (cp *OCIConveyorPacker) fetch(ctx context.Context) error {
// cp.srcRef contains the cache source reference
_, err := copy.Image(ctx, cp.policyCtx, cp.tmpfsRef, cp.srcRef, &copy.Options{
ReportWriter: io.Discard,
ReportWriter: io.Discard,
// nolint:staticcheck
SourceCtx: ocitransport.SystemContextFromTransportOptions(cp.topts),
RemoveSignatures: true,
})
return err
}

func (cp *OCIConveyorPacker) getConfig(ctx context.Context) (imgspecv1.ImageConfig, error) {
// nolint:staticcheck
img, err := cp.srcRef.NewImage(ctx, ocitransport.SystemContextFromTransportOptions(cp.topts))
if err != nil {
return imgspecv1.ImageConfig{}, err
Expand Down Expand Up @@ -410,6 +412,7 @@ func (cp *OCIConveyorPacker) extractArchive(src string, dst string) error {
}

func (cp *OCIConveyorPacker) unpackTmpfs(ctx context.Context) error {
// nolint:staticcheck
return unpackRootfs(ctx, cp.b, cp.tmpfsRef, ocitransport.SystemContextFromTransportOptions(cp.topts))
}

Expand Down
5 changes: 2 additions & 3 deletions internal/pkg/ocitransport/ocitransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,12 @@ func (t *TransportOptions) SystemContext() types.SystemContext {
}

// TransportOptionsFromSystemContext returns a TransportOptions struct
// initialized from a containers/image SystemContext. If the SystemContext is
// nil, then nil is returned.
// initialized from a containers/image SystemContext.
//
// Deprecated: for containers/image compatibility only. To be removed in future
func TransportOptionsFromSystemContext(sc *types.SystemContext) *TransportOptions {
if sc == nil {
return nil
sc = defaultSysCtx()
}

if sc.OSChoice == "" || sc.ArchitectureChoice == "" {
Expand Down

0 comments on commit 4838e0c

Please sign in to comment.