...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/generate-makefile/generate_container_image_makefile.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/generate-makefile

     1  // Copyright 2023 Chaos Mesh Authors.
     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 main
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os"
    21  	"text/template"
    22  
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // containerImageGeneratedMkTemplate is the template for the file container-image.generated.mk, use containerImageGeneratedMkOptions as the context.
    27  const containerImageGeneratedMkTemplate = `# Generated by ./cmd/generate-makefile. DO NOT EDIT.
    28  
    29  ##@ Generated targets in container-image.generated.mk
    30  
    31  .PHONY: image
    32  image-all: {{- range .Recipes }} image-{{.ImageName}} {{- end }} ## Build all container images
    33  
    34  {{ .Content -}}
    35  
    36  .PHONY: clean-image-built
    37  clean-image-built:
    38  {{- range .Recipes }}
    39  	rm -f {{ .SourcePath }}/.dockerbuilt
    40  {{- end }}
    41  
    42  `
    43  
    44  // containerImageGeneratedMkTemplate is the template for one target, use containerImageRecipeOptions as the context.
    45  const containerImageRecipeTemplate = `.PHONY: image-{{ .ImageName }}
    46  image-{{ .ImageName }}: {{ .SourcePath }}/.dockerbuilt ## {{ .Comment }}
    47  
    48  {{ .SourcePath }}/.dockerbuilt: SHELL=bash
    49  {{ .SourcePath }}/.dockerbuilt: {{ StringsJoin .DependencyTargets " " }} {{ .SourcePath }}/Dockerfile
    50  	$(ROOT)/build/build_image.py {{ .ImageName }} {{ .SourcePath }}
    51  	touch {{ .SourcePath }}/.dockerbuilt
    52  
    53  `
    54  
    55  func renderContainerImageGeneratedMk() error {
    56  	targetFile, err := os.OpenFile("container-image.generated.mk", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
    57  	if err != nil {
    58  		return errors.Wrap(err, "open file container-image.generated.mk")
    59  	}
    60  	recipeTemplate, err := template.New("container-image.generated.mk recipe").Funcs(defaultFuncMap).Parse(containerImageRecipeTemplate)
    61  	if err != nil {
    62  		return errors.Wrap(err, "parse container-image.generated.mk recipe")
    63  	}
    64  	var buffer bytes.Buffer
    65  	for _, recipe := range containerImageRecipes {
    66  		err := recipeTemplate.Execute(&buffer, recipe)
    67  		if err != nil {
    68  			return errors.Wrap(err, fmt.Sprintf("render recipe in container-image.generated.mk, recipe: image-%s", recipe.ImageName))
    69  		}
    70  	}
    71  
    72  	containerImageTemplate, err := template.New("container-image.generated.mk").Funcs(defaultFuncMap).Parse(containerImageGeneratedMkTemplate)
    73  	if err != nil {
    74  		return errors.Wrap(err, "parse container-image.generated.mk template")
    75  	}
    76  	err = containerImageTemplate.Execute(targetFile, containerImageGeneratedMkOptions{
    77  		Recipes: containerImageRecipes,
    78  		Content: buffer.String(),
    79  	})
    80  	if err != nil {
    81  		return errors.Wrap(err, "render container-image.generated.mk")
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  type containerImageRecipeOptions struct {
    88  	ImageName  string
    89  	SourcePath string
    90  	// DependencyTargets are the targets that this target depends on.
    91  	DependencyTargets []string
    92  	// Comment is the comment for the target, do not need to include the leading `##`
    93  	Comment string
    94  }
    95  
    96  type containerImageGeneratedMkOptions struct {
    97  	Recipes []containerImageRecipeOptions
    98  	Content string
    99  }
   100  
   101  var containerImageRecipes = []containerImageRecipeOptions{
   102  	{
   103  		ImageName:  "chaos-daemon",
   104  		SourcePath: "images/chaos-daemon",
   105  		DependencyTargets: []string{
   106  			"images/chaos-daemon/bin/chaos-daemon",
   107  			"images/chaos-daemon/bin/pause",
   108  			"images/chaos-daemon/bin/cdh",
   109  		},
   110  		Comment: "Build container image for chaos-daemon, ghcr.io/chaos-mesh/chaos-daemon:latest",
   111  	}, {
   112  		ImageName:         "chaos-mesh",
   113  		SourcePath:        "images/chaos-mesh",
   114  		DependencyTargets: []string{"images/chaos-mesh/bin/chaos-controller-manager"},
   115  		Comment:           "Build container image for chaos-mesh, ghcr.io/chaos-mesh/chaos-mesh:latest",
   116  	}, {
   117  		ImageName:         "chaos-dashboard",
   118  		SourcePath:        "images/chaos-dashboard",
   119  		DependencyTargets: []string{"images/chaos-dashboard/bin/chaos-dashboard"},
   120  		Comment:           "Build container image for chaos-dashboard, ghcr.io/chaos-mesh/chaos-dashboard:latest",
   121  	}, {
   122  		ImageName:         "build-env",
   123  		SourcePath:        "images/build-env",
   124  		DependencyTargets: nil,
   125  		Comment:           "Build container image for build-env, ghcr.io/chaos-mesh/build-env:latest",
   126  	}, {
   127  		ImageName:         "dev-env",
   128  		SourcePath:        "images/dev-env",
   129  		DependencyTargets: nil,
   130  		Comment:           "Build container image for dev-env, ghcr.io/chaos-mesh/dev-env:latest",
   131  	}, {
   132  		ImageName:         "e2e-helper",
   133  		SourcePath:        "e2e-test/cmd/e2e_helper",
   134  		DependencyTargets: nil,
   135  		Comment:           "Build container image for e2e-helper",
   136  	}, {
   137  		ImageName:  "chaos-mesh-e2e",
   138  		SourcePath: "e2e-test/image/e2e",
   139  		DependencyTargets: []string{
   140  			"e2e-test/image/e2e/manifests",
   141  			"e2e-test/image/e2e/chaos-mesh",
   142  			"e2e-build",
   143  		},
   144  		Comment: "Build container image for running e2e tests",
   145  	}, {
   146  		ImageName:         "chaos-kernel",
   147  		SourcePath:        "images/chaos-kernel",
   148  		DependencyTargets: nil,
   149  		Comment:           "Build container image for chaos-kernel, ghcr.io/chaos-mesh/chaos-kernel:latest",
   150  	}, {
   151  		ImageName:         "chaos-dlv",
   152  		SourcePath:        "images/chaos-dlv",
   153  		DependencyTargets: nil,
   154  		Comment:           "Build container image for chaos-dlv",
   155  	},
   156  }
   157