github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/define/image/v1/sealer_image.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v1
    16  
    17  import (
    18  	"encoding/json"
    19  
    20  	applicationV1 "github.com/sealerio/sealer/pkg/define/application/v1"
    21  	"github.com/sealerio/sealer/pkg/define/application/version"
    22  	apiv1 "github.com/sealerio/sealer/types/api/v1"
    23  
    24  	ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
    25  )
    26  
    27  const (
    28  	SealerImageExtension          = "sealer.image.extension"
    29  	SealerImageContainerImageList = "sealer.image.container.images"
    30  
    31  	// Kube image type
    32  	KubeInstaller = "kube-installer"
    33  	AppInstaller  = "app-installer"
    34  
    35  	ImageSpecSchemaVersionV1Beta1 = "v1alpha1"
    36  )
    37  
    38  type ImageSpec struct {
    39  	// ID the image id
    40  	ID string `json:"id"`
    41  
    42  	// Name the image name
    43  	Name string `json:"name"`
    44  
    45  	// Digest the image digest
    46  	Digest string `json:"digest"`
    47  
    48  	// ManifestV1 OCI version v1 image manifest
    49  	ManifestV1 ociv1.Manifest `json:"manifestv1"`
    50  
    51  	// OCIv1 OCI version v1 image spec
    52  	OCIv1 ociv1.Image `json:"ociv1"`
    53  
    54  	ImageExtension ImageExtension `json:"imageExtension"`
    55  
    56  	// ContainerImageList the container image list contained in the sealer image
    57  	//
    58  	// TODO: populate this value during the build phase
    59  	ContainerImageList []*ContainerImage `json:"containerImageList,omitempty"`
    60  }
    61  
    62  type ContainerImage struct {
    63  	// Image the container image name
    64  	Image string `json:"image" yaml:"image"`
    65  
    66  	// AppName the mame of the app to which the container image belongs
    67  	//
    68  	// NOTE: A container image may not belong to any app. In this case, the appName value is null.
    69  	AppName string `json:"appName,omitempty" yaml:"appName"`
    70  
    71  	// Platform the container image platform
    72  	Platform *apiv1.Platform `json:"platform" yaml:"platform"`
    73  
    74  	// TODO: add more info about container image if necessary such as resourceKind, resourceName, etc.
    75  }
    76  
    77  func GetImageSliceFromContainerImageList(containerImageList []*ContainerImage) []string {
    78  	var images []string
    79  	for _, containerImage := range containerImageList {
    80  		images = append(images, containerImage.Image)
    81  	}
    82  	return images
    83  }
    84  
    85  // NOTE: the UnmarshalJSON function of ImageExtension has been overrode
    86  type ImageExtension struct {
    87  	// BuildClient build client info
    88  	BuildClient BuildClient `json:"buildClient"`
    89  
    90  	// image spec schema version
    91  	SchemaVersion string `json:"schemaVersion"`
    92  
    93  	// sealer image type, like AppImage
    94  	Type string `json:"type,omitempty"`
    95  
    96  	// applications in the sealer image
    97  	Applications []version.VersionedApplication `json:"applications,omitempty"`
    98  
    99  	// Labels are metadata to the sealer image
   100  	Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
   101  
   102  	// Env is a set of key value pair.
   103  	// set to sealer image some default parameters which is in global level.
   104  	// user could overwrite it through v2.ClusterSpec at run stage.
   105  	Env map[string]string `json:"env,omitempty"`
   106  
   107  	// launch spec will declare
   108  	Launch Launch `json:"launch,omitempty"`
   109  }
   110  
   111  type BuildClient struct {
   112  	SealerVersion string `json:"sealerVersion"`
   113  
   114  	BuildahVersion string `json:"buildahVersion"`
   115  }
   116  
   117  type Launch struct {
   118  	Cmds []string `json:"cmds,omitempty"`
   119  
   120  	// user specified LAUNCH instruction
   121  	AppNames []string `json:"app_names,omitempty"`
   122  }
   123  
   124  type v1ImageExtension struct {
   125  	// BuildClient build client info
   126  	BuildClient BuildClient `json:"buildClient"`
   127  	// image spec schema version
   128  	SchemaVersion string `json:"schemaVersion"`
   129  	// sealer image type, like AppImage
   130  	Type string `json:"type,omitempty"`
   131  	// Labels are metadata to the sealer image
   132  	Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
   133  	// applications in the sealer image
   134  	Applications []applicationV1.Application `json:"applications,omitempty"`
   135  	// launch spec will declare
   136  	Launch Launch `json:"launch,omitempty"`
   137  	// Env global env
   138  	Env map[string]string `json:"env,omitempty"`
   139  }
   140  
   141  func (ie *ImageExtension) UnmarshalJSON(data []byte) error {
   142  	*ie = ImageExtension{}
   143  	v1Ex := v1ImageExtension{}
   144  	if err := json.Unmarshal(data, &v1Ex); err != nil {
   145  		return err
   146  	}
   147  
   148  	(*ie).BuildClient = v1Ex.BuildClient
   149  	(*ie).SchemaVersion = v1Ex.SchemaVersion
   150  	(*ie).Labels = v1Ex.Labels
   151  	(*ie).Env = v1Ex.Env
   152  	(*ie).Type = v1Ex.Type
   153  	(*ie).Applications = make([]version.VersionedApplication, len(v1Ex.Applications))
   154  	for i, app := range v1Ex.Applications {
   155  		tmpApp := app
   156  		(*ie).Applications[i] = &tmpApp
   157  	}
   158  	(*ie).Launch = v1Ex.Launch
   159  	return nil
   160  }