sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/internal/templates/dockerfile.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package templates
    18  
    19  import (
    20  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    21  )
    22  
    23  var _ machinery.Template = &Dockerfile{}
    24  
    25  // Dockerfile scaffolds a file that defines the containerized build process
    26  type Dockerfile struct {
    27  	machinery.TemplateMixin
    28  }
    29  
    30  // SetTemplateDefaults implements file.Template
    31  func (f *Dockerfile) SetTemplateDefaults() error {
    32  	if f.Path == "" {
    33  		f.Path = "Dockerfile"
    34  	}
    35  
    36  	f.TemplateBody = dockerfileTemplate
    37  
    38  	return nil
    39  }
    40  
    41  const dockerfileTemplate = `# Build the manager binary
    42  FROM golang:1.13 as builder
    43  
    44  WORKDIR /workspace
    45  # Copy the Go Modules manifests
    46  COPY go.mod go.mod
    47  COPY go.sum go.sum
    48  # cache deps before building and copying source so that we don't need to re-download as much
    49  # and so that source changes don't invalidate our downloaded layer
    50  RUN go mod download
    51  
    52  # Copy the go source
    53  COPY main.go main.go
    54  COPY api/ api/
    55  COPY controllers/ controllers/
    56  
    57  # Build
    58  RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
    59  
    60  # Use distroless as minimal base image to package the manager binary
    61  # Refer to https://github.com/GoogleContainerTools/distroless for more details
    62  FROM gcr.io/distroless/static:nonroot
    63  WORKDIR /
    64  COPY --from=builder /workspace/manager .
    65  USER nonroot:nonroot
    66  
    67  ENTRYPOINT ["/manager"]
    68  `