/
helper.go
40 lines (35 loc) · 1.12 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
package miner
import (
"github.com/caarlos0/env/v8"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type MinerEnv struct {
Image *string `env:"IMAGE"`
}
// HasMatchingLabels verifies that the Label Selector matches the given Labels.
func HasMatchingLabels(matchSelector metav1.LabelSelector, matchLabels map[string]string) bool {
// This should never fail, validating webhook should catch this first
selector, err := metav1.LabelSelectorAsSelector(&matchSelector)
if err != nil {
return false
}
// If a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() {
return false
}
if !selector.Matches(labels.Set(matchLabels)) {
return false
}
return true
}
func GetMinerEnv() MinerEnv {
var minerEnv MinerEnv
_ = env.ParseWithOptions(&minerEnv, env.Options{Prefix: "ONEX_"})
return minerEnv
}