github.com/IBM-Blockchain/fabric-operator@v1.0.4/operatorconfig/operator.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 operatorconfig
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/IBM-Blockchain/fabric-operator/pkg/apis/common"
    25  	"github.com/IBM-Blockchain/fabric-operator/pkg/apis/deployer"
    26  	cainit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/enroller"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/container"
    29  	"github.com/vrischmann/envconfig"
    30  
    31  	corev1 "k8s.io/api/core/v1"
    32  	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
    33  
    34  	"sigs.k8s.io/yaml"
    35  )
    36  
    37  // Client defines interface for making GET calls to kubernetes API server
    38  type Client interface {
    39  	Get(ctx context.Context, key k8sclient.ObjectKey, obj k8sclient.Object) error
    40  }
    41  
    42  // LoadFromConfigMap will read config map and return back operator config built on top by
    43  // updating config values based on environment variables.
    44  func LoadFromConfigMap(nn k8sclient.ObjectKey, key string, client Client, operator *Operator) error {
    45  	cm := &corev1.ConfigMap{}
    46  	err := client.Get(context.TODO(), nn, cm)
    47  	if k8sclient.IgnoreNotFound(err) != nil {
    48  		return err
    49  	}
    50  
    51  	err = load([]byte(cm.Data[key]), operator)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func load(config []byte, operator *Operator) error {
    60  	// If no config bytes passed, we can assume that a config file (config map) for operator
    61  	// does not exist and we can skip the unmarshal step.
    62  	if config != nil && len(config) > 0 {
    63  		if err := yaml.Unmarshal(config, operator); err != nil {
    64  			return err
    65  		}
    66  	}
    67  
    68  	opts := envconfig.Options{
    69  		Prefix:      "IBPOPERATOR",
    70  		AllOptional: true,
    71  		LeaveNil:    true,
    72  	}
    73  
    74  	if err := envconfig.InitWithOptions(operator, opts); err != nil {
    75  		return err
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  // Operator defines operator configuration parameters
    82  type Operator struct {
    83  	Orderer  Orderer            `json:"orderer" yaml:"orderer"`
    84  	Peer     Peer               `json:"peer" yaml:"peer"`
    85  	CA       CA                 `json:"ca" yaml:"ca"`
    86  	Console  Console            `json:"console" yaml:"console"`
    87  	Restart  Restart            `json:"restart" yaml:"restart"`
    88  	Versions *deployer.Versions `json:"versions,omitempty" yaml:"versions,omitempty"`
    89  	Globals  Globals            `json:"globals,omitempty" yaml:"globals,omitempty" envconfig:"optional"`
    90  	Debug    Debug              `json:"debug" yaml:"debug"`
    91  }
    92  
    93  // CA defines configurable properties for CA custom resource
    94  type CA struct {
    95  	Timeouts CATimeouts `json:"timeouts" yaml:"timeouts"`
    96  }
    97  
    98  // CATimeouts defines timeouts properties that can be configured
    99  type CATimeouts struct {
   100  	HSMInitJob cainit.HSMInitJobTimeouts `json:"hsmInitJob" yaml:"hsmInitJob"`
   101  }
   102  
   103  type Orderer struct {
   104  	Timeouts      OrdererTimeouts `json:"timeouts" yaml:"timeouts"`
   105  	Renewals      OrdererRenewals `json:"renewals" yaml:"renewals"`
   106  	DisableProbes string          `json:"disableProbes" yaml:"disableProbes"`
   107  }
   108  
   109  type OrdererTimeouts struct {
   110  	SecretPoll common.Duration               `json:"secretPollTimeout" yaml:"secretPollTimeout"`
   111  	EnrollJob  enroller.HSMEnrollJobTimeouts `json:"enrollJob" yaml:"enrollJob"`
   112  }
   113  
   114  type OrdererRenewals struct {
   115  	DisableTLScert bool `json:"disableTLScert" yaml:"disableTLScert"`
   116  }
   117  
   118  type Peer struct {
   119  	Timeouts PeerTimeouts `json:"timeouts" yaml:"timeouts"`
   120  }
   121  
   122  type PeerTimeouts struct {
   123  	DBMigration DBMigrationTimeouts           `json:"dbMigration" yaml:"dbMigration"`
   124  	EnrollJob   enroller.HSMEnrollJobTimeouts `json:"enrollJob" yaml:"enrollJob"`
   125  }
   126  
   127  type DBMigrationTimeouts struct {
   128  	CouchDBStartUp common.Duration `json:"couchDBStartUp" yaml:"couchDbStartUp"`
   129  	JobStart       common.Duration `json:"jobStart" yaml:"jobStart"`
   130  	JobCompletion  common.Duration `json:"jobCompletion" yaml:"jobCompletion"`
   131  	ReplicaChange  common.Duration `json:"replicaChange" yaml:"replicaChange"`
   132  	PodDeletion    common.Duration `json:"podDeletion" yaml:"podDeletion"`
   133  	PodStart       common.Duration `json:"podStart" yaml:"podStart"`
   134  }
   135  
   136  type Restart struct {
   137  	WaitTime common.Duration `json:"waitTime" yaml:"waitTime"`
   138  	Disable  DisableRestart  `json:"disable" yaml:"disable"`
   139  	Timeout  common.Duration `json:"timeout" yaml:"timeout"`
   140  }
   141  
   142  type DisableRestart struct {
   143  	Components bool `json:"components" yaml:"components"`
   144  }
   145  
   146  type Globals struct {
   147  	SecurityContext         *container.SecurityContext `json:"securityContext,omitempty" yaml:"securityContext,omitempty"`
   148  	AllowKubernetesEighteen string                     `json:"allowKubernetesEighteen,omitempty" yaml:"allowKubernetesEighteen,omitempty"`
   149  }
   150  
   151  type Debug struct {
   152  	DisableDeploymentChecks string `json:"disableDeploymentChecks,omitempty" yaml:"disableDeploymentChecks,omitempty"`
   153  }
   154  
   155  type Console struct {
   156  	ApplyNetworkPolicy string `json:"applyNetworkPolicy" yaml:"applyNetworkPolicy"`
   157  }
   158  
   159  // SetDefaults will set defaults as defined by to the operator configuration settings
   160  func (o *Operator) SetDefaults() {
   161  	*o = Operator{
   162  		Orderer: Orderer{
   163  			Timeouts: OrdererTimeouts{
   164  				SecretPoll: common.MustParseDuration("30s"),
   165  				EnrollJob: enroller.HSMEnrollJobTimeouts{
   166  					JobStart:      common.MustParseDuration("90s"),
   167  					JobCompletion: common.MustParseDuration("90s"),
   168  				},
   169  			},
   170  		},
   171  		Peer: Peer{
   172  			Timeouts: PeerTimeouts{
   173  				DBMigration: DBMigrationTimeouts{
   174  					CouchDBStartUp: common.MustParseDuration("90s"),
   175  					JobStart:       common.MustParseDuration("90s"),
   176  					JobCompletion:  common.MustParseDuration("90s"),
   177  					ReplicaChange:  common.MustParseDuration("90s"),
   178  					PodDeletion:    common.MustParseDuration("90s"),
   179  					PodStart:       common.MustParseDuration("90s"),
   180  				},
   181  				EnrollJob: enroller.HSMEnrollJobTimeouts{
   182  					JobStart:      common.MustParseDuration("90s"),
   183  					JobCompletion: common.MustParseDuration("90s"),
   184  				},
   185  			},
   186  		},
   187  		CA: CA{
   188  			Timeouts: CATimeouts{
   189  				HSMInitJob: cainit.HSMInitJobTimeouts{
   190  					JobStart:      common.MustParseDuration("90s"),
   191  					JobCompletion: common.MustParseDuration("90s"),
   192  				},
   193  			},
   194  		},
   195  		Restart: Restart{
   196  			WaitTime: common.MustParseDuration("10m"),
   197  			Timeout:  common.MustParseDuration("5m"),
   198  		},
   199  		Versions: getDefaultVersions(),
   200  	}
   201  }