...

Source file src/github.com/linkerd/linkerd2/controller/proxy-injector/fake/factory.go

Documentation: github.com/linkerd/linkerd2/controller/proxy-injector/fake

     1  package fake
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	admissionv1beta1 "k8s.io/api/admission/v1beta1"
     8  	appsv1 "k8s.io/api/apps/v1"
     9  	corev1 "k8s.io/api/core/v1"
    10  	"sigs.k8s.io/yaml"
    11  )
    12  
    13  // These constants provide default, fake strings for testing proxy-injector.
    14  const (
    15  	DefaultControllerNamespace = "linkerd"
    16  	DefaultNamespace           = "default"
    17  )
    18  
    19  // Factory is a factory that can convert in-file YAML content into Kubernetes
    20  // API objects.
    21  type Factory struct {
    22  	rootDir string
    23  }
    24  
    25  // NewFactory returns a new instance of Fixture.
    26  func NewFactory(rootDir string) *Factory {
    27  	return &Factory{rootDir: rootDir}
    28  }
    29  
    30  // FileContents returns the content of the specified file as a slice of
    31  // bytes. If the file doesn't exist in the 'fake/data' folder, an error will be
    32  // returned.
    33  func (f *Factory) FileContents(filename string) ([]byte, error) {
    34  	return os.ReadFile(filepath.Join(f.rootDir, filename))
    35  }
    36  
    37  // AdmissionReview returns the content of the specified file as an
    38  // AdmissionReview type. An error will be returned if:
    39  // i. the file doesn't exist in the 'fake/data' folder or,
    40  // ii. the file content isn't a valid YAML structure that can be unmarshalled
    41  // into AdmissionReview type
    42  func (f *Factory) AdmissionReview(filename string) (*admissionv1beta1.AdmissionReview, error) {
    43  	b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	var admissionReview admissionv1beta1.AdmissionReview
    48  	if err := yaml.Unmarshal(b, &admissionReview); err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	return &admissionReview, nil
    53  }
    54  
    55  // Deployment returns the content of the specified file as a Deployment type. An
    56  // error will be returned if:
    57  // i. the file doesn't exist in the 'fake/data' folder or
    58  // ii. the file content isn't a valid YAML structure that can be unmarshalled
    59  // into Deployment type
    60  func (f *Factory) Deployment(filename string) (*appsv1.Deployment, error) {
    61  	b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	var deployment appsv1.Deployment
    67  	if err := yaml.Unmarshal(b, &deployment); err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	return &deployment, nil
    72  }
    73  
    74  // Container returns the content of the specified file as a Container type. An
    75  // error will be returned if:
    76  // i. the file doesn't exist in the 'fake/data' folder or
    77  // ii. the file content isn't a valid YAML structure that can be unmarshalled
    78  // into Container type
    79  func (f *Factory) Container(filename string) (*corev1.Container, error) {
    80  	b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	var container corev1.Container
    86  	if err := yaml.Unmarshal(b, &container); err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	return &container, nil
    91  }
    92  
    93  // ConfigMap returns the content of the specified file as a ConfigMap type. An
    94  // error will be returned if:
    95  // i. the file doesn't exist in the 'fake/data' folder or
    96  // ii. the file content isn't a valid YAML structure that can be unmarshalled
    97  // into ConfigMap type
    98  func (f *Factory) ConfigMap(filename string) (*corev1.ConfigMap, error) {
    99  	b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	var configMap corev1.ConfigMap
   105  	if err := yaml.Unmarshal(b, &configMap); err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	return &configMap, nil
   110  }
   111  
   112  // Namespace returns the content of the specified file as a Namespace type. An
   113  // error will be returned if:
   114  // i. the file doesn't exist in the 'fake/data' folder or
   115  // ii. the file content isn't a valid YAML structure that can be unmarshalled
   116  // into Namespace type
   117  func (f *Factory) Namespace(filename string) (*corev1.Namespace, error) {
   118  	b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	var namespace corev1.Namespace
   124  	if err := yaml.Unmarshal(b, &namespace); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return &namespace, nil
   129  }
   130  
   131  // Volume returns the content of the specified file as a Volume type. An error
   132  // will be returned if:
   133  // i. the file doesn't exist in the 'fake/data' folder or
   134  // ii. the file content isn't a valid YAML structure that can be unmarshalled
   135  // into Volume type
   136  func (f *Factory) Volume(filename string) (*corev1.Volume, error) {
   137  	b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	var volume corev1.Volume
   143  	if err := yaml.Unmarshal(b, &volume); err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return &volume, nil
   148  }
   149  

View as plain text