github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/base/ca/override/pvc.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  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    23  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  )
    28  
    29  func (o *Override) PVC(object v1.Object, pvc *corev1.PersistentVolumeClaim, action resources.Action) error {
    30  	instance := object.(*current.IBPCA)
    31  	switch action {
    32  	case resources.Create:
    33  		return o.CreatePVC(instance, pvc)
    34  	case resources.Update:
    35  		return o.UpdatePVC(instance, pvc)
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func (o *Override) CreatePVC(instance *current.IBPCA, pvc *corev1.PersistentVolumeClaim) error {
    42  	storage := instance.Spec.Storage
    43  	if storage != nil {
    44  		caStorage := storage.CA
    45  		if caStorage != nil {
    46  			if caStorage.Class != "" {
    47  				pvc.Spec.StorageClassName = &caStorage.Class
    48  			}
    49  			if caStorage.Size != "" {
    50  				quantity, err := resource.ParseQuantity(caStorage.Size)
    51  				if err != nil {
    52  					return err
    53  				}
    54  				resourceMap := pvc.Spec.Resources.Requests
    55  				if resourceMap == nil {
    56  					resourceMap = corev1.ResourceList{}
    57  				}
    58  				resourceMap[corev1.ResourceStorage] = quantity
    59  				pvc.Spec.Resources.Requests = resourceMap
    60  			}
    61  		}
    62  	}
    63  
    64  	if pvc.ObjectMeta.Labels == nil {
    65  		pvc.ObjectMeta.Labels = map[string]string{}
    66  	}
    67  	if instance.Spec.Zone != "" {
    68  		pvc.ObjectMeta.Labels["zone"] = instance.Spec.Zone
    69  	}
    70  
    71  	if instance.Spec.Region != "" {
    72  		pvc.ObjectMeta.Labels["region"] = instance.Spec.Region
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (o *Override) UpdatePVC(instance *current.IBPCA, pvc *corev1.PersistentVolumeClaim) error {
    79  	return nil
    80  }