github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/global/config.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package global
    20  
    21  import (
    22  	config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
    23  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/container"
    24  	ibpdep "github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/deployment"
    25  	ibpjob "github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/job"
    26  
    27  	appsv1 "k8s.io/api/apps/v1"
    28  	batchv1 "k8s.io/api/batch/v1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  )
    31  
    32  // ConfigSetter sets values on all resources created by operator
    33  type ConfigSetter struct {
    34  	Config config.Globals
    35  }
    36  
    37  type resource interface {
    38  	UpdateSecurityContextForAllContainers(sc container.SecurityContext)
    39  }
    40  
    41  // Apply applies all global configurations
    42  func (cs *ConfigSetter) Apply(obj runtime.Object) {
    43  	cs.UpdateSecurityContextForAllContainers(obj)
    44  }
    45  
    46  // UpdateSecurityContextForAllContainers updates the security context for all containers defined on
    47  // resource object
    48  func (cs *ConfigSetter) UpdateSecurityContextForAllContainers(obj runtime.Object) {
    49  	if cs.Config.SecurityContext == nil {
    50  		return
    51  	}
    52  
    53  	var resource resource
    54  	switch obj.(type) {
    55  	case *appsv1.Deployment:
    56  		resource = ibpdep.New(obj.(*appsv1.Deployment))
    57  	case *batchv1.Job:
    58  		resource = ibpjob.NewWithDefaults(obj.(*batchv1.Job))
    59  	default:
    60  		return
    61  	}
    62  
    63  	resource.UpdateSecurityContextForAllContainers(*cs.Config.SecurityContext)
    64  }