github.com/oam-dev/kubevela@v1.9.11/pkg/utils/apply/share.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package apply
    18  
    19  import (
    20  	"strings"
    21  
    22  	"k8s.io/utils/strings/slices"
    23  
    24  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    25  )
    26  
    27  const (
    28  	sharedBySep = ","
    29  )
    30  
    31  // AddSharer add sharer
    32  func AddSharer(sharedBy string, app *v1beta1.Application) string {
    33  	key := GetAppKey(app)
    34  	sharers := strings.Split(sharedBy, sharedBySep)
    35  	existing := slices.Contains(sharers, key)
    36  	if !existing {
    37  		sharers = append(slices.Filter(nil, sharers, func(s string) bool {
    38  			return s != ""
    39  		}), key)
    40  	}
    41  	return strings.Join(sharers, sharedBySep)
    42  }
    43  
    44  // ContainsSharer check if the shared-by annotation contains the target application
    45  func ContainsSharer(sharedBy string, app *v1beta1.Application) bool {
    46  	key := GetAppKey(app)
    47  	sharers := strings.Split(sharedBy, sharedBySep)
    48  	return slices.Contains(sharers, key)
    49  }
    50  
    51  // FirstSharer get the first sharer of the application
    52  func FirstSharer(sharedBy string) string {
    53  	sharers := strings.Split(sharedBy, sharedBySep)
    54  	return sharers[0]
    55  }
    56  
    57  // RemoveSharer remove sharer
    58  func RemoveSharer(sharedBy string, app *v1beta1.Application) string {
    59  	key := GetAppKey(app)
    60  	sharers := strings.Split(sharedBy, sharedBySep)
    61  	sharers = slices.Filter(nil, sharers, func(s string) bool {
    62  		return s != key
    63  	})
    64  	return strings.Join(sharers, sharedBySep)
    65  }