@@ -27,6 +27,7 @@ import (
27
27
"github.com/replicatedhq/kots/pkg/imageutil"
28
28
"github.com/replicatedhq/kots/pkg/kotsutil"
29
29
"github.com/replicatedhq/kots/pkg/logger"
30
+ kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
30
31
oras "oras.land/oras-go/v2"
31
32
orasfile "oras.land/oras-go/v2/content/file"
32
33
orasremote "oras.land/oras-go/v2/registry/remote"
@@ -108,9 +109,9 @@ func WriteProgressLine(progressWriter io.Writer, line string) {
108
109
}
109
110
110
111
// CopyAirgapImages pushes images found in the airgap bundle/airgap root to the configured registry.
111
- func CopyAirgapImages (opts imagetypes.ProcessImageOptions , log * logger.CLILogger ) ( * imagetypes. CopyAirgapImagesResult , error ) {
112
+ func CopyAirgapImages (opts imagetypes.ProcessImageOptions , log * logger.CLILogger ) error {
112
113
if opts .AirgapBundle == "" {
113
- return & imagetypes. CopyAirgapImagesResult {}, nil
114
+ return nil
114
115
}
115
116
116
117
pushOpts := imagetypes.PushImagesOptions {
@@ -125,27 +126,25 @@ func CopyAirgapImages(opts imagetypes.ProcessImageOptions, log *logger.CLILogger
125
126
LogForUI : true ,
126
127
}
127
128
128
- copyResult , err := TagAndPushImagesFromBundle (opts .AirgapBundle , pushOpts )
129
+ err := TagAndPushImagesFromBundle (opts .AirgapBundle , pushOpts )
129
130
if err != nil {
130
- return nil , errors .Wrap (err , "failed to push images from bundle" )
131
+ return errors .Wrap (err , "failed to push images from bundle" )
131
132
}
132
133
133
- return & imagetypes.CopyAirgapImagesResult {
134
- EmbeddedClusterArtifacts : copyResult .EmbeddedClusterArtifacts ,
135
- }, nil
134
+ return nil
136
135
}
137
136
138
- func TagAndPushImagesFromBundle (airgapBundle string , options imagetypes.PushImagesOptions ) ( * imagetypes. CopyAirgapImagesResult , error ) {
137
+ func TagAndPushImagesFromBundle (airgapBundle string , options imagetypes.PushImagesOptions ) error {
139
138
airgap , err := kotsutil .FindAirgapMetaInBundle (airgapBundle )
140
139
if err != nil {
141
- return nil , errors .Wrap (err , "failed to find airgap meta" )
140
+ return errors .Wrap (err , "failed to find airgap meta" )
142
141
}
143
142
144
143
switch airgap .Spec .Format {
145
144
case dockertypes .FormatDockerRegistry :
146
145
extractedBundle , err := os .MkdirTemp ("" , "extracted-airgap-kots" )
147
146
if err != nil {
148
- return nil , errors .Wrap (err , "failed to create temp dir for unarchived airgap bundle" )
147
+ return errors .Wrap (err , "failed to create temp dir for unarchived airgap bundle" )
149
148
}
150
149
defer os .RemoveAll (extractedBundle )
151
150
@@ -155,34 +154,32 @@ func TagAndPushImagesFromBundle(airgapBundle string, options imagetypes.PushImag
155
154
},
156
155
}
157
156
if err := tarGz .Unarchive (airgapBundle , extractedBundle ); err != nil {
158
- return nil , errors .Wrap (err , "falied to unarchive airgap bundle" )
157
+ return errors .Wrap (err , "falied to unarchive airgap bundle" )
159
158
}
160
159
if err := PushImagesFromTempRegistry (extractedBundle , airgap .Spec .SavedImages , options ); err != nil {
161
- return nil , errors .Wrap (err , "failed to push images from docker registry bundle" )
160
+ return errors .Wrap (err , "failed to push images from docker registry bundle" )
162
161
}
163
162
case dockertypes .FormatDockerArchive , "" :
164
163
if err := PushImagesFromDockerArchiveBundle (airgapBundle , options ); err != nil {
165
- return nil , errors .Wrap (err , "failed to push images from docker archive bundle" )
164
+ return errors .Wrap (err , "failed to push images from docker archive bundle" )
166
165
}
167
166
default :
168
- return nil , errors .Errorf ("Airgap bundle format '%s' is not supported" , airgap .Spec .Format )
167
+ return errors .Errorf ("Airgap bundle format '%s' is not supported" , airgap .Spec .Format )
169
168
}
170
169
171
170
pushEmbeddedArtifactsOpts := imagetypes.PushEmbeddedClusterArtifactsOptions {
172
- Registry : options .Registry ,
173
- Tag : imageutil .SanitizeTag (fmt .Sprintf ("%s-%s-%s" , airgap .Spec .ChannelID , airgap .Spec .UpdateCursor , airgap .Spec .VersionLabel )),
174
- HTTPClient : orasretry .DefaultClient ,
171
+ Registry : options .Registry ,
172
+ ChannelID : airgap .Spec .ChannelID ,
173
+ UpdateCursor : airgap .Spec .UpdateCursor ,
174
+ VersionLabel : airgap .Spec .VersionLabel ,
175
+ HTTPClient : orasretry .DefaultClient ,
175
176
}
176
- pushedArtifacts , err : = PushEmbeddedClusterArtifacts (airgapBundle , pushEmbeddedArtifactsOpts )
177
+ err = PushEmbeddedClusterArtifacts (airgapBundle , airgap . Spec . EmbeddedClusterArtifacts , pushEmbeddedArtifactsOpts )
177
178
if err != nil {
178
- return nil , errors .Wrap (err , "failed to push embedded cluster artifacts" )
179
- }
180
-
181
- result := & imagetypes.CopyAirgapImagesResult {
182
- EmbeddedClusterArtifacts : pushedArtifacts ,
179
+ return errors .Wrap (err , "failed to push embedded cluster artifacts" )
183
180
}
184
181
185
- return result , nil
182
+ return nil
186
183
}
187
184
188
185
func PushImagesFromTempRegistry (airgapRootDir string , imageList []string , options imagetypes.PushImagesOptions ) error {
@@ -684,70 +681,75 @@ func reportWriterWithProgress(imageInfos map[string]*imagetypes.ImageInfo, repor
684
681
return pipeWriter
685
682
}
686
683
687
- func PushEmbeddedClusterArtifacts (airgapBundle string , opts imagetypes.PushEmbeddedClusterArtifactsOptions ) ([] string , error ) {
684
+ func PushEmbeddedClusterArtifacts (airgapBundle string , artifactsToPush * kotsv1beta1. EmbeddedClusterArtifacts , opts imagetypes.PushEmbeddedClusterArtifactsOptions ) error {
688
685
tmpDir , err := os .MkdirTemp ("" , "embedded-cluster-artifacts" )
689
686
if err != nil {
690
- return nil , errors .Wrap (err , "failed to create temp directory" )
687
+ return errors .Wrap (err , "failed to create temp directory" )
691
688
}
692
689
defer os .RemoveAll (tmpDir )
693
690
694
691
fileReader , err := os .Open (airgapBundle )
695
692
if err != nil {
696
- return nil , errors .Wrap (err , "failed to open file" )
693
+ return errors .Wrap (err , "failed to open file" )
697
694
}
698
695
defer fileReader .Close ()
699
696
700
697
gzipReader , err := gzip .NewReader (fileReader )
701
698
if err != nil {
702
- return nil , errors .Wrap (err , "failed to get new gzip reader" )
699
+ return errors .Wrap (err , "failed to get new gzip reader" )
703
700
}
704
701
defer gzipReader .Close ()
705
702
706
703
var artifacts []string
707
704
708
705
tarReader := tar .NewReader (gzipReader )
709
- pushedArtifacts := make ([]string , 0 )
710
706
for {
711
707
header , err := tarReader .Next ()
712
708
if err == io .EOF {
713
709
break
714
710
}
715
711
if err != nil {
716
- return nil , errors .Wrap (err , "failed to get read archive" )
712
+ return errors .Wrap (err , "failed to get read archive" )
717
713
}
718
714
719
715
if header .Typeflag != tar .TypeReg {
720
716
continue
721
717
}
722
718
723
- if filepath . Dir (header .Name ) != "embedded-cluster" {
719
+ if ! shouldPushArtifact (header .Name , artifactsToPush ) {
724
720
continue
725
721
}
726
722
727
723
dstFilePath := filepath .Join (tmpDir , header .Name )
728
724
if err := os .MkdirAll (filepath .Dir (dstFilePath ), 0755 ); err != nil {
729
- return nil , errors .Wrap (err , "failed to create path" )
725
+ return errors .Wrap (err , "failed to create path" )
730
726
}
731
727
732
728
dstFile , err := os .Create (dstFilePath )
733
729
if err != nil {
734
- return nil , errors .Wrap (err , "failed to create file" )
730
+ return errors .Wrap (err , "failed to create file" )
735
731
}
736
732
737
733
if _ , err := io .Copy (dstFile , tarReader ); err != nil {
738
734
dstFile .Close ()
739
- return nil , errors .Wrap (err , "failed to copy file data" )
735
+ return errors .Wrap (err , "failed to copy file data" )
740
736
}
741
737
742
738
dstFile .Close ()
743
739
artifacts = append (artifacts , dstFilePath )
744
740
}
745
741
746
742
for i , dstFilePath := range artifacts {
747
- name := filepath .Base (dstFilePath )
748
- repository := filepath .Join ("embedded-cluster" , imageutil .SanitizeRepo (name ))
743
+ ociArtifactPath := imageutil .NewEmbeddedClusterOCIArtifactPath (dstFilePath , imageutil.EmbeddedClusterArtifactOCIPathOptions {
744
+ RegistryHost : opts .Registry .Endpoint ,
745
+ RegistryNamespace : opts .Registry .Namespace ,
746
+ ChannelID : opts .ChannelID ,
747
+ UpdateCursor : opts .UpdateCursor ,
748
+ VersionLabel : opts .VersionLabel ,
749
+ })
750
+
749
751
artifactFile := imagetypes.OCIArtifactFile {
750
- Name : name ,
752
+ Name : ociArtifactPath . Name ,
751
753
Path : dstFilePath ,
752
754
MediaType : EmbeddedClusterMediaType ,
753
755
}
@@ -756,20 +758,31 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
756
758
Files : []imagetypes.OCIArtifactFile {artifactFile },
757
759
ArtifactType : EmbeddedClusterArtifactType ,
758
760
Registry : opts .Registry ,
759
- Repository : repository ,
760
- Tag : opts .Tag ,
761
+ Repository : ociArtifactPath . Repository ,
762
+ Tag : ociArtifactPath .Tag ,
761
763
HTTPClient : opts .HTTPClient ,
762
764
}
763
765
764
766
fmt .Printf ("Pushing embedded cluster artifacts (%d/%d)\n " , i + 1 , len (artifacts ))
765
- artifact := fmt .Sprintf ("%s:%s" , filepath .Join (opts .Registry .Endpoint , opts .Registry .Namespace , repository ), opts .Tag )
766
767
if err := pushOCIArtifact (pushOCIArtifactOpts ); err != nil {
767
- return nil , errors .Wrapf (err , "failed to push oci artifact %s" , name )
768
+ return errors .Wrapf (err , "failed to push oci artifact %s" , ociArtifactPath . Name )
768
769
}
769
- pushedArtifacts = append (pushedArtifacts , artifact )
770
770
}
771
771
772
- return pushedArtifacts , nil
772
+ return nil
773
+ }
774
+
775
+ func shouldPushArtifact (artifactPath string , artifactsToPush * kotsv1beta1.EmbeddedClusterArtifacts ) bool {
776
+ if artifactsToPush == nil {
777
+ return false
778
+ }
779
+
780
+ switch artifactPath {
781
+ case artifactsToPush .BinaryAmd64 , artifactsToPush .Charts , artifactsToPush .ImagesAmd64 , artifactsToPush .Metadata :
782
+ return true
783
+ default :
784
+ return false
785
+ }
773
786
}
774
787
775
788
func pushOCIArtifact (opts imagetypes.PushOCIArtifactOptions ) error {
0 commit comments