blob: 073bb255c92bcc42a7ff64e448bf3897dd15f409 [file] [log] [blame]
Kbowers27c05422023-11-20 08:53:21 +01001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Davide Salernoa0dbb492022-11-24 10:32:52 +010019
20package utils
21
22import (
Davide Salernoa0dbb492022-11-24 10:32:52 +010023 "os"
Ricardo Zaninif01e6c72023-02-15 16:52:08 -030024
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
Ricardo Zaniniccfaf5a2023-10-16 13:42:53 -030027 "github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata"
Ricardo Zaninif01e6c72023-02-15 16:52:08 -030028)
29
30const (
Ricardo Zanini4f505a72024-05-08 10:59:02 -030031 // DefaultServicePortName default service name to increase compatibility with Knative
32 //
33 // see: https://github.com/knative/specs/blob/main/specs/serving/runtime-contract.md#protocols-and-ports
34 // By default we do support HTTP/2:https://quarkus.io/guides/http-reference#http2-support
35 DefaultServicePortName = "h2c"
Davide Salernoa0dbb492022-11-24 10:32:52 +010036)
37
38// GetOperatorIDAnnotation to safely get the operator id annotation value.
39func GetOperatorIDAnnotation(obj metav1.Object) string {
40 if obj == nil || obj.GetAnnotations() == nil {
41 return ""
42 }
43
Ricardo Zaninif01e6c72023-02-15 16:52:08 -030044 if operatorId, ok := obj.GetAnnotations()[metadata.OperatorIDAnnotation]; ok {
Davide Salernoa0dbb492022-11-24 10:32:52 +010045 return operatorId
46 }
47
48 return ""
49}
50
51func OperatorID() string {
Ricardo Zaninif01e6c72023-02-15 16:52:08 -030052 // TODO: what's this KAMEL_ ?
Davide Salernoa0dbb492022-11-24 10:32:52 +010053 return envOrDefault("", "KAMEL_OPERATOR_ID", "OPERATOR_ID")
54}
55
56func envOrDefault(def string, envs ...string) string {
57 for i := range envs {
58 if val := os.Getenv(envs[i]); val != "" {
59 return val
60 }
61 }
62
63 return def
64}
65
66// Pbool returns a pointer to a boolean
67func Pbool(b bool) *bool {
68 return &b
69}
70
Ricardo Zaninif7664c32023-05-29 07:23:29 -030071// Pint returns a pointer to an int
72func Pint(i int32) *int32 {
73 return &i
Davide Salernoa0dbb492022-11-24 10:32:52 +010074}
75
76func Compare(a, b []byte) bool {
77 a = append(a, b...)
78 c := 0
79 for _, x := range a {
80 c ^= int(x)
81 }
82 return c == 0
83}
Davide Salerno2cf38592023-05-26 10:51:12 +020084
85func GetEnv(key, fallback string) string {
86 value := os.Getenv(key)
87 if len(value) == 0 {
88 return fallback
89 }
90 return value
91}