sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/declarative/v1/scaffolds/api.go (about)

     1  /*
     2  Copyright 2022 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 scaffolds
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  
    23  	"github.com/spf13/afero"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    27  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    28  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
    29  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/scaffolds/internal/templates"
    30  )
    31  
    32  const (
    33  	exampleManifestVersion = "0.0.1"
    34  )
    35  
    36  var _ plugins.Scaffolder = &apiScaffolder{}
    37  
    38  type apiScaffolder struct {
    39  	config   config.Config
    40  	resource resource.Resource
    41  
    42  	// fs is the filesystem that will be used by the scaffolder
    43  	fs machinery.Filesystem
    44  }
    45  
    46  // NewAPIScaffolder returns a new Scaffolder for declarative
    47  func NewAPIScaffolder(config config.Config, res resource.Resource) plugins.Scaffolder {
    48  	return &apiScaffolder{
    49  		config:   config,
    50  		resource: res,
    51  	}
    52  }
    53  
    54  // InjectFS implements cmdutil.Scaffolder
    55  func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
    56  	s.fs = fs
    57  }
    58  
    59  // Scaffold implements cmdutil.Scaffolder
    60  func (s *apiScaffolder) Scaffold() error {
    61  	// Load the boilerplate
    62  	boilerplate, err := afero.ReadFile(s.fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
    63  	if err != nil {
    64  		return fmt.Errorf("error updating scaffold: unable to load boilerplate: %w", err)
    65  	}
    66  
    67  	// Initialize the machinery.Scaffold that will write the files to disk
    68  	scaffold := machinery.NewScaffold(s.fs,
    69  		machinery.WithConfig(s.config),
    70  		machinery.WithBoilerplate(string(boilerplate)),
    71  		machinery.WithResource(&s.resource),
    72  	)
    73  
    74  	//nolint:staticcheck
    75  	err = scaffold.Execute(
    76  		&templates.Types{IsLegacyLayout: plugin.IsLegacyLayout(s.config)},
    77  		&templates.Controller{IsLegacyLayout: plugin.IsLegacyLayout(s.config)},
    78  		&templates.Channel{ManifestVersion: exampleManifestVersion},
    79  		&templates.Manifest{ManifestVersion: exampleManifestVersion},
    80  	)
    81  	if err != nil {
    82  		return fmt.Errorf("error updating scaffold: %w", err)
    83  	}
    84  	return nil
    85  }