github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/base/console/override/consolecm.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 override
    20  
    21  import (
    22  	"errors"
    23  	"fmt"
    24  
    25  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    26  
    27  	consolev1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/console/v1"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    29  	corev1 "k8s.io/api/core/v1"
    30  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"sigs.k8s.io/yaml"
    32  )
    33  
    34  func (o *Override) ConsoleCM(object v1.Object, cm *corev1.ConfigMap, action resources.Action, options map[string]interface{}) error {
    35  	instance := object.(*current.IBPConsole)
    36  	switch action {
    37  	case resources.Create:
    38  		return o.CreateConsoleCM(instance, cm, options)
    39  	case resources.Update:
    40  		return o.UpdateConsoleCM(instance, cm, options)
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  func (o *Override) CreateConsoleCM(instance *current.IBPConsole, cm *corev1.ConfigMap, options map[string]interface{}) error {
    47  	return errors.New("no create console cm defined, this needs to implemented")
    48  }
    49  
    50  func (o *Override) UpdateConsoleCM(instance *current.IBPConsole, cm *corev1.ConfigMap, options map[string]interface{}) error {
    51  	data := cm.Data["settings.yaml"]
    52  
    53  	config := &consolev1.ConsoleSettingsConfig{}
    54  	err := yaml.Unmarshal([]byte(data), config)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	err = CommonConsoleCM(instance, config, options)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	bytes, err := yaml.Marshal(config)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	if cm.Data == nil {
    70  		cm.Data = map[string]string{}
    71  	}
    72  
    73  	cm.Data["settings.yaml"] = string(bytes)
    74  	return nil
    75  }
    76  
    77  func CommonConsoleCM(instance *current.IBPConsole, config *consolev1.ConsoleSettingsConfig, options map[string]interface{}) error {
    78  	config.DynamicConfig = true
    79  	config.IBMID = instance.Spec.IBMID
    80  	config.IAMApiKey = instance.Spec.IAMApiKey
    81  	config.SegmentWriteKey = instance.Spec.SegmentWriteKey
    82  	config.TrustProxy = "loopback, linklocal, uniquelocal"
    83  
    84  	if instance.Spec.Email != "" {
    85  		config.Email = instance.Spec.Email
    86  	}
    87  
    88  	if instance.Spec.AuthScheme != "" {
    89  		config.AuthScheme = instance.Spec.AuthScheme
    90  	}
    91  
    92  	if instance.Spec.AllowDefaultPassword {
    93  		config.AllowDefaultPassword = true
    94  	}
    95  
    96  	if instance.Spec.ConfigtxlatorURL != "" {
    97  		config.Configtxlator = instance.Spec.ConfigtxlatorURL
    98  	}
    99  
   100  	if instance.Spec.DeployerURL != "" {
   101  		config.DeployerURL = instance.Spec.DeployerURL
   102  	}
   103  
   104  	if instance.Spec.DeployerTimeout != 0 {
   105  		config.DeployerTimeout = instance.Spec.DeployerTimeout
   106  	}
   107  
   108  	if instance.Spec.Components != "" {
   109  		config.DBCustomNames.Components = instance.Spec.Components
   110  	}
   111  
   112  	if instance.Spec.Sessions != "" {
   113  		config.DBCustomNames.Sessions = instance.Spec.Sessions
   114  	}
   115  
   116  	if instance.Spec.System != "" {
   117  		config.DBCustomNames.System = instance.Spec.System
   118  	}
   119  
   120  	if instance.Spec.SystemChannel != "" {
   121  		config.SystemChannelID = instance.Spec.SystemChannel
   122  	}
   123  
   124  	// ensures a default value
   125  	if instance.Spec.Proxying == nil {
   126  		t := true
   127  		instance.Spec.Proxying = &t
   128  	}
   129  
   130  	if *instance.Spec.Proxying {
   131  		config.ProxyTLSReqs = "always"
   132  	}
   133  
   134  	if instance.Spec.FeatureFlags != nil {
   135  		config.Featureflags = instance.Spec.FeatureFlags
   136  	} else {
   137  		config.Featureflags = &consolev1.FeatureFlags{
   138  			ReadOnlyEnabled:         new(bool),
   139  			ImportOnlyEnabled:       new(bool),
   140  			CreateChannelEnabled:    true,
   141  			RemotePeerConfigEnabled: true,
   142  			TemplatesEnabled:        false,
   143  			CapabilitiesEnabled:     true,
   144  			HighAvailability:        true,
   145  			EnableNodeOU:            true,
   146  			HSMEnabled:              true,
   147  			ScaleRaftNodesEnabled:   true,
   148  			Lifecycle20Enabled:      true,
   149  			Patch14to20Enabled:      true,
   150  			MustgatherEnabled:       true,
   151  			InfraImportOptions: &consolev1.InfraImportOptions{
   152  				SupportedCAs:      []string{OPENSHIFT, K8S},
   153  				SupportedOrderers: []string{OPENSHIFT, K8S},
   154  				SupportedPeers:    []string{OPENSHIFT, K8S},
   155  			},
   156  		}
   157  	}
   158  
   159  	if instance.Spec.ClusterData != nil {
   160  		config.ClusterData = instance.Spec.ClusterData
   161  	} else {
   162  		config.ClusterData = &consolev1.IBPConsoleClusterData{}
   163  	}
   164  
   165  	if config.ClusterData.Type == "" {
   166  		config.ClusterData.Type = "paid"
   167  	}
   168  
   169  	crn := instance.Spec.CRN
   170  	if crn != nil {
   171  		config.CRN = &consolev1.CRN{
   172  			Version:      crn.Version,
   173  			CName:        crn.CName,
   174  			CType:        crn.CType,
   175  			Servicename:  crn.Servicename,
   176  			Location:     crn.Location,
   177  			AccountID:    crn.AccountID,
   178  			InstanceID:   crn.InstanceID,
   179  			ResourceType: crn.ResourceType,
   180  			ResourceID:   crn.ResourceID,
   181  		}
   182  		config.CRNString = fmt.Sprintf("crn:%s:%s:%s:%s:%s:%s:%s:%s:%s",
   183  			crn.Version, crn.CName, crn.CType, crn.Servicename, crn.Location, crn.AccountID, crn.InstanceID, crn.ResourceType, crn.ResourceID)
   184  	}
   185  
   186  	consoleOverrides, err := instance.Spec.GetOverridesConsole()
   187  	if err != nil {
   188  		return err
   189  	}
   190  
   191  	if consoleOverrides.ActivityTrackerConsolePath != "" {
   192  		config.ActivityTrackerPath = consoleOverrides.ActivityTrackerConsolePath
   193  	}
   194  
   195  	// This field is to indicate if the new way of setting up HSM
   196  	// with init sidecar is enabled
   197  	if consoleOverrides.HSM != "" && consoleOverrides.HSM != "false" {
   198  		config.HSM = "true"
   199  	}
   200  
   201  	if options != nil && options["username"] != nil && options["password"] != nil {
   202  		config.DeployerURL = fmt.Sprintf("http://%s:%s@localhost:8080", options["username"].(string), options["password"].(string))
   203  	}
   204  
   205  	return nil
   206  }