github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/base/console/override/deployercm.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  	"encoding/json"
    23  	"errors"
    24  
    25  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    26  	"github.com/IBM-Blockchain/fabric-operator/defaultconfig/console"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/apis/deployer"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/util/image"
    30  	corev1 "k8s.io/api/core/v1"
    31  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"sigs.k8s.io/yaml"
    33  )
    34  
    35  func (o *Override) DeployerCM(object v1.Object, cm *corev1.ConfigMap, action resources.Action, options map[string]interface{}) error {
    36  	instance := object.(*current.IBPConsole)
    37  	switch action {
    38  	case resources.Create:
    39  		return o.CreateDeployerCM(instance, cm, options)
    40  	case resources.Update:
    41  		return o.UpdateDeployerCM(instance, cm, options)
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func (o *Override) CreateDeployerCM(instance *current.IBPConsole, cm *corev1.ConfigMap, options map[string]interface{}) error {
    48  	return errors.New("no create deployer cm defined, this needs to implemented")
    49  }
    50  
    51  func (o *Override) UpdateDeployerCM(instance *current.IBPConsole, cm *corev1.ConfigMap, options map[string]interface{}) error {
    52  	data := cm.Data["settings.yaml"]
    53  
    54  	config := &deployer.Config{}
    55  	err := yaml.Unmarshal([]byte(data), config)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	err = CommonDeployerCM(instance, config, options)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	bytes, err := yaml.Marshal(config)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	if cm.Data == nil {
    71  		cm.Data = map[string]string{}
    72  	}
    73  
    74  	cm.Data["settings.yaml"] = string(bytes)
    75  
    76  	return nil
    77  }
    78  
    79  func CommonDeployerCM(instance *current.IBPConsole, config *deployer.Config, options map[string]interface{}) error {
    80  	if len(instance.Spec.ImagePullSecrets) == 0 {
    81  		return errors.New("no image pull secret provided")
    82  	}
    83  
    84  	if instance.Spec.NetworkInfo == nil || instance.Spec.NetworkInfo.Domain == "" {
    85  		return errors.New("no domain provided")
    86  	}
    87  
    88  	config.ImagePullSecrets = instance.Spec.ImagePullSecrets
    89  	config.Domain = instance.Spec.NetworkInfo.Domain
    90  
    91  	if instance.Spec.Deployer != nil {
    92  		if instance.Spec.Deployer.CreateDB {
    93  			config.Database.CreateDB = instance.Spec.Deployer.CreateDB
    94  		}
    95  		if instance.Spec.Deployer.ComponentsDB != "" {
    96  			config.Database.Components.Name = instance.Spec.Deployer.ComponentsDB
    97  		}
    98  		if instance.Spec.Deployer.ConnectionString != "" {
    99  			config.Database.ConnectionURL = instance.Spec.Deployer.ConnectionString
   100  		}
   101  	}
   102  
   103  	registryURL := instance.Spec.RegistryURL
   104  	arch := "amd64"
   105  	if instance.Spec.Arch != nil && len(instance.Spec.Arch) > 0 {
   106  		arch = instance.Spec.Arch[0]
   107  	}
   108  
   109  	if instance.Spec.UseTags != nil && instance.Spec.UseTags != config.UseTags {
   110  		config.UseTags = instance.Spec.UseTags
   111  	}
   112  
   113  	requestedVersions := &deployer.Versions{}
   114  	if instance.Spec.Versions != nil {
   115  		// convert spec version to deployer config versions
   116  		instanceVersionBytes, err := json.Marshal(instance.Spec.Versions)
   117  		if err != nil {
   118  			return err
   119  		}
   120  		err = json.Unmarshal(instanceVersionBytes, requestedVersions)
   121  		if err != nil {
   122  			return err
   123  		}
   124  	} else {
   125  		// use default config versions
   126  		requestedVersions = config.Versions
   127  	}
   128  	config.Versions.Override(requestedVersions, registryURL, arch)
   129  
   130  	images := instance.Spec.Images
   131  	if images == nil {
   132  		images = &current.ConsoleImages{}
   133  	}
   134  	defaultimage := console.GetImages()
   135  
   136  	// TODO:OSS what happens if defaultimage is empty
   137  	mustgatherImage := image.GetImage(registryURL, defaultimage.MustgatherImage, images.MustgatherImage)
   138  	mustgatherTag := image.GetTag(arch, defaultimage.MustgatherTag, images.MustgatherTag)
   139  
   140  	config.OtherImages = &deployer.OtherImages{
   141  		MustgatherImage: mustgatherImage,
   142  		MustgatherTag:   mustgatherTag,
   143  	}
   144  
   145  	config.ServiceAccount = instance.GetName()
   146  
   147  	storageClassName := ""
   148  	if instance.Spec.Storage != nil && instance.Spec.Storage.Console != nil {
   149  		storageClassName = instance.Spec.Storage.Console.Class
   150  	}
   151  
   152  	config.Defaults.Storage.CA.CA.Class = storageClassName
   153  	config.Defaults.Storage.Peer.Peer.Class = storageClassName
   154  	config.Defaults.Storage.Peer.StateDB.Class = storageClassName
   155  	config.Defaults.Storage.Orderer.Orderer.Class = storageClassName
   156  
   157  	crn := instance.Spec.CRN
   158  	if crn != nil {
   159  		config.CRN = &current.CRN{
   160  			Version:      crn.Version,
   161  			CName:        crn.CName,
   162  			CType:        crn.CType,
   163  			Servicename:  crn.Servicename,
   164  			Location:     crn.Location,
   165  			AccountID:    crn.AccountID,
   166  			InstanceID:   crn.InstanceID,
   167  			ResourceType: crn.ResourceType,
   168  			ResourceID:   crn.ResourceID,
   169  		}
   170  	}
   171  
   172  	// used for passing separate domains for optools and deployer
   173  	if instance.Spec.Deployer != nil && instance.Spec.Deployer.Domain != "" {
   174  		config.Domain = instance.Spec.Deployer.Domain
   175  	}
   176  
   177  	deployerOverrides, err := instance.Spec.GetOverridesDeployer()
   178  	if err != nil {
   179  		return err
   180  	}
   181  	if deployerOverrides != nil && deployerOverrides.Timeouts != nil {
   182  		config.Timeouts = &deployer.Timeouts{}
   183  		if deployerOverrides.Timeouts.APIServer != 0 {
   184  			config.Timeouts.APIServer = deployerOverrides.Timeouts.APIServer
   185  		}
   186  		if deployerOverrides.Timeouts.Deployment != 0 {
   187  			config.Timeouts.Deployment = deployerOverrides.Timeouts.Deployment
   188  		}
   189  	}
   190  
   191  	if options != nil && options["username"] != nil && options["password"] != nil {
   192  		config.Auth.Username = options["username"].(string)
   193  		config.Auth.Password = options["password"].(string)
   194  	}
   195  
   196  	return nil
   197  }