@@ -57,15 +57,15 @@ func buildImageConfig(opts BuildOpts) ocispec.ImageConfig {
5757 return cfg
5858}
5959
60- // DetectRootfsType inspects path and returns the detected RootfsType.
61- func DetectRootfsType (path string ) (RootfsType , error ) {
60+ // DetectSourceType inspects path and returns the detected RootfsType.
61+ func DetectSourceType (path string ) (kraftfile. SourceType , error ) {
6262 if path == "" {
6363 return "" , fmt .Errorf ("empty rootfs path" )
6464 }
6565
6666 base := filepath .Base (path )
6767 if base == "Dockerfile" || slices .Contains (strings .Split (base , "." ), "Dockerfile" ) {
68- return RootfsTypeDockerfile , nil
68+ return kraftfile . SourceTypeDockerfile , nil
6969 }
7070
7171 fi , err := os .Stat (path )
@@ -78,13 +78,13 @@ func DetectRootfsType(path string) (RootfsType, error) {
7878
7979 switch {
8080 case fi .IsDir ():
81- return RootfsTypeDir , nil
81+ return kraftfile . SourceTypeDirectory , nil
8282 case fi .Mode ().IsRegular (), fi .Mode ()& os .ModeSymlink != 0 :
8383 if gocpio .IsValidPath (path ) {
84- return RootfsTypeCpio , nil
84+ return kraftfile . SourceTypeCpio , nil
8585 }
8686 if goerofs .IsValidPath (path ) {
87- return RootfsTypeErofs , nil
87+ return kraftfile . SourceTypeErofs , nil
8888 }
8989 return "" , fmt .Errorf ("could not detect file rootfs type %q" , path )
9090 default :
@@ -93,30 +93,28 @@ func DetectRootfsType(path string) (RootfsType, error) {
9393}
9494
9595// BuildRootfs builds a rootfs for each platform in opts.Platform from the
96- // source at opts.Rootfs.Path. The source is detected in this order:
97- //
98- // 1. A pre-packaged rootfs file - returned as-is.
99- // 2. A directory - walked and archived.
100- // 3. Anything else - treated as a Dockerfile context and built with BuildKit.
96+ // source at opts.Rootfs.Path.
10197func BuildRootfs (ctx context.Context , opts BuildOpts ) (_ []* imagespec.Image , rerr error ) {
10298 if len (opts .Platform ) == 0 {
10399 return nil , fmt .Errorf ("at least one platform must be specified" )
104100 }
105101
106- if opts .Rootfs .Type == "" {
107- typ , err := DetectRootfsType (opts .Rootfs .Path )
108- if err != nil {
109- return nil , err
110- }
111- opts .Rootfs .Type = typ
112- }
113-
114102 switch opts .Rootfs .Type {
115- case RootfsTypeCpio , RootfsTypeErofs :
103+ case kraftfile .SourceTypeCpio , kraftfile .SourceTypeErofs :
104+ if opts .Rootfs .Format != "" {
105+ expected := map [kraftfile.SourceType ]kraftfile.FsType {
106+ kraftfile .SourceTypeCpio : kraftfile .FsTypeCpio ,
107+ kraftfile .SourceTypeErofs : kraftfile .FsTypeErofs ,
108+ }
109+ if exp , ok := expected [opts .Rootfs .Type ]; ok && opts .Rootfs .Format != exp {
110+ // TODO: maybe we could be smart and convert this
111+ return nil , fmt .Errorf ("unsupported rootfs format mismatch: source is %s but requested format is %s" , opts .Rootfs .Type , opts .Rootfs .Format )
112+ }
113+ }
116114 return buildRootfsPackaged (ctx , opts )
117- case RootfsTypeDir :
115+ case kraftfile . SourceTypeDirectory :
118116 return buildRootfsDirectory (ctx , opts )
119- case RootfsTypeDockerfile :
117+ case kraftfile . SourceTypeDockerfile :
120118 if f , err := os .Stat (opts .Rootfs .Path ); err != nil {
121119 if os .IsNotExist (err ) {
122120 return nil , fmt .Errorf ("dockerfile does not exist" )
@@ -127,7 +125,7 @@ func BuildRootfs(ctx context.Context, opts BuildOpts) (_ []*imagespec.Image, rer
127125 }
128126 return buildRootfsDockerfile (ctx , opts )
129127 default :
130- return nil , fmt .Errorf ("unknown rootfs type %q" , opts .Rootfs .Type )
128+ return nil , fmt .Errorf ("unsupported rootfs type %q" , opts .Rootfs .Type )
131129 }
132130}
133131
0 commit comments