Kbowers | 27c0542 | 2023-11-20 08:53:21 +0100 | [diff] [blame] | 1 | /* |
| 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 Salerno | a0dbb49 | 2022-11-24 10:32:52 +0100 | [diff] [blame] | 19 | |
| 20 | package utils |
| 21 | |
| 22 | import ( |
Davide Salerno | a0dbb49 | 2022-11-24 10:32:52 +0100 | [diff] [blame] | 23 | "os" |
Ricardo Zanini | f01e6c7 | 2023-02-15 16:52:08 -0300 | [diff] [blame] | 24 | |
| 25 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | |
Ricardo Zanini | ccfaf5a | 2023-10-16 13:42:53 -0300 | [diff] [blame] | 27 | "github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata" |
Ricardo Zanini | f01e6c7 | 2023-02-15 16:52:08 -0300 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | const ( |
Ricardo Zanini | 4f505a7 | 2024-05-08 10:59:02 -0300 | [diff] [blame] | 31 | // 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 Salerno | a0dbb49 | 2022-11-24 10:32:52 +0100 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | // GetOperatorIDAnnotation to safely get the operator id annotation value. |
| 39 | func GetOperatorIDAnnotation(obj metav1.Object) string { |
| 40 | if obj == nil || obj.GetAnnotations() == nil { |
| 41 | return "" |
| 42 | } |
| 43 | |
Ricardo Zanini | f01e6c7 | 2023-02-15 16:52:08 -0300 | [diff] [blame] | 44 | if operatorId, ok := obj.GetAnnotations()[metadata.OperatorIDAnnotation]; ok { |
Davide Salerno | a0dbb49 | 2022-11-24 10:32:52 +0100 | [diff] [blame] | 45 | return operatorId |
| 46 | } |
| 47 | |
| 48 | return "" |
| 49 | } |
| 50 | |
| 51 | func OperatorID() string { |
Ricardo Zanini | f01e6c7 | 2023-02-15 16:52:08 -0300 | [diff] [blame] | 52 | // TODO: what's this KAMEL_ ? |
Davide Salerno | a0dbb49 | 2022-11-24 10:32:52 +0100 | [diff] [blame] | 53 | return envOrDefault("", "KAMEL_OPERATOR_ID", "OPERATOR_ID") |
| 54 | } |
| 55 | |
| 56 | func 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 |
| 67 | func Pbool(b bool) *bool { |
| 68 | return &b |
| 69 | } |
| 70 | |
Ricardo Zanini | f7664c3 | 2023-05-29 07:23:29 -0300 | [diff] [blame] | 71 | // Pint returns a pointer to an int |
| 72 | func Pint(i int32) *int32 { |
| 73 | return &i |
Davide Salerno | a0dbb49 | 2022-11-24 10:32:52 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func 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 Salerno | 2cf3859 | 2023-05-26 10:51:12 +0200 | [diff] [blame] | 84 | |
| 85 | func GetEnv(key, fallback string) string { |
| 86 | value := os.Getenv(key) |
| 87 | if len(value) == 0 { |
| 88 | return fallback |
| 89 | } |
| 90 | return value |
| 91 | } |