github.com/openebs/api@v1.12.0/pkg/kubernetes/core/service.go (about)

     1  // Copyright © 2020 The OpenEBS 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 core
    16  
    17  import (
    18  	corev1 "k8s.io/api/core/v1"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  )
    21  
    22  // Service holds the api's service objects
    23  type Service struct {
    24  	*corev1.Service
    25  }
    26  
    27  // NewService returns new instance of service
    28  func NewService() *Service {
    29  	return &Service{
    30  		&corev1.Service{},
    31  	}
    32  }
    33  
    34  // WithName sets the Name field of service with provided value.
    35  func (s *Service) WithName(name string) *Service {
    36  	s.Name = name
    37  	return s
    38  }
    39  
    40  // WithNamespace sets the Namespace field of Service with provided value.
    41  func (s *Service) WithNamespace(namespace string) *Service {
    42  
    43  	s.Namespace = namespace
    44  	return s
    45  }
    46  
    47  // WithAnnotations merges existing annotations if any
    48  // with the ones that are provided here
    49  func (s *Service) WithAnnotations(annotations map[string]string) *Service {
    50  	for key, value := range annotations {
    51  		s.Annotations[key] = value
    52  	}
    53  	return s
    54  }
    55  
    56  // WithAnnotationsNew resets the annotation field of service
    57  // with provided arguments
    58  func (s *Service) WithAnnotationsNew(annotations map[string]string) *Service {
    59  	newannotations := make(map[string]string)
    60  	for key, value := range annotations {
    61  		newannotations[key] = value
    62  	}
    63  	s.Annotations = newannotations
    64  	return s
    65  }
    66  
    67  // WithLabels merges existing labels if any
    68  // with the ones that are provided here
    69  func (s *Service) WithLabels(labels map[string]string) *Service {
    70  	if s.Labels == nil {
    71  		return s.WithLabelsNew(labels)
    72  	}
    73  
    74  	for key, value := range labels {
    75  		s.Labels[key] = value
    76  	}
    77  	return s
    78  }
    79  
    80  // WithLabelsNew resets the labels field of service
    81  // with provided arguments
    82  func (s *Service) WithLabelsNew(labels map[string]string) *Service {
    83  	newLabels := make(map[string]string)
    84  	for key, value := range labels {
    85  		newLabels[key] = value
    86  	}
    87  
    88  	s.Labels = newLabels
    89  	return s
    90  }
    91  
    92  // WithSelectors merges existing selectors if any
    93  // with the ones that are provided here
    94  func (s *Service) WithSelectors(selectors map[string]string) *Service {
    95  	if s.Spec.Selector == nil {
    96  		return s.WithSelectorsNew(selectors)
    97  	}
    98  
    99  	for key, value := range selectors {
   100  		s.Spec.Selector[key] = value
   101  	}
   102  	return s
   103  }
   104  
   105  // WithSelectorsNew resets existing selectors if any with
   106  // ones that are provided here
   107  func (s *Service) WithSelectorsNew(selectors map[string]string) *Service {
   108  	// copy of original map
   109  	newslctrs := map[string]string{}
   110  	for key, value := range selectors {
   111  		newslctrs[key] = value
   112  	}
   113  
   114  	// override
   115  	s.Spec.Selector = newslctrs
   116  	return s
   117  }
   118  
   119  // WithPorts sets the Ports field of Service with provided arguments
   120  func (s *Service) WithPorts(ports []corev1.ServicePort) *Service {
   121  	// copy of original slice
   122  	newports := []corev1.ServicePort{}
   123  	newports = append(newports, ports...)
   124  
   125  	// override
   126  	s.Spec.Ports = newports
   127  	return s
   128  }
   129  
   130  // WithType sets the Type field of Service with provided arguments
   131  func (s *Service) WithType(svcType corev1.ServiceType) *Service {
   132  	s.Spec.Type = svcType
   133  	return s
   134  }
   135  
   136  // WithOwnerReferenceNew sets ownerrefernce if any with
   137  // ones that are provided here
   138  func (s *Service) WithOwnerReferenceNew(ownerRefernce []metav1.OwnerReference) *Service {
   139  
   140  	s.OwnerReferences = ownerRefernce
   141  	return s
   142  }
   143  
   144  // Build returns the Service API instance
   145  func (s *Service) Build() *corev1.Service {
   146  	return s.Service
   147  }