istio

Форк
0
/
resource.go 
119 строк · 3.2 Кб
1
/*
2
 Copyright Istio 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 status
18

19
import (
20
	"context"
21
	"fmt"
22
	"strconv"
23
	"strings"
24

25
	"k8s.io/apimachinery/pkg/runtime/schema"
26

27
	"istio.io/api/meta/v1alpha1"
28
	"istio.io/istio/pkg/config"
29
	"istio.io/istio/pkg/config/resource"
30
	"istio.io/istio/pkg/config/schema/collections"
31
	"istio.io/istio/pkg/config/schema/gvk"
32
	"istio.io/istio/pkg/log"
33
)
34

35
var scope = log.RegisterScope("status",
36
	"status controller for istio")
37

38
func ResourceFromString(s string) *Resource {
39
	pieces := strings.Split(s, "/")
40
	if len(pieces) != 6 {
41
		scope.Errorf("cannot unmarshal %s into resource identifier", s)
42
		return nil
43
	}
44
	return &Resource{
45
		GroupVersionResource: schema.GroupVersionResource{
46
			Group:    pieces[0],
47
			Version:  pieces[1],
48
			Resource: pieces[2],
49
		},
50
		Namespace:  pieces[3],
51
		Name:       pieces[4],
52
		Generation: pieces[5],
53
	}
54
}
55

56
// TODO: maybe replace with a kubernetes resource identifier, if that's a thing
57
type Resource struct {
58
	schema.GroupVersionResource
59
	Namespace  string
60
	Name       string
61
	Generation string
62
}
63

64
func (r Resource) String() string {
65
	return strings.Join([]string{r.Group, r.Version, r.GroupVersionResource.Resource, r.Namespace, r.Name, r.Generation}, "/")
66
}
67

68
func (r *Resource) ToModelKey() string {
69
	// we have a resource here, but model keys use kind.  Use the schema to find the correct kind.
70
	found, _ := collections.All.FindByGroupVersionResource(r.GroupVersionResource)
71
	return config.Key(
72
		found.Group(), found.Version(), found.Kind(),
73
		r.Name, r.Namespace)
74
}
75

76
func ResourceFromMetadata(i resource.Metadata) Resource {
77
	return Resource{
78
		GroupVersionResource: i.Schema.GroupVersionResource(),
79
		Namespace:            i.FullName.Namespace.String(),
80
		Name:                 i.FullName.Name.String(),
81
		Generation:           strconv.FormatInt(i.Generation, 10),
82
	}
83
}
84

85
func ResourceFromModelConfig(c config.Config) Resource {
86
	gvr, ok := gvk.ToGVR(c.GroupVersionKind)
87
	if !ok {
88
		return Resource{}
89
	}
90
	return Resource{
91
		GroupVersionResource: gvr,
92
		Namespace:            c.Namespace,
93
		Name:                 c.Name,
94
		Generation:           strconv.FormatInt(c.Generation, 10),
95
	}
96
}
97

98
func GetTypedStatus(in any) (out *v1alpha1.IstioStatus, err error) {
99
	if ret, ok := in.(*v1alpha1.IstioStatus); ok {
100
		return ret, nil
101
	}
102
	return nil, fmt.Errorf("cannot cast %T: %v to IstioStatus", in, in)
103
}
104

105
func GetOGProvider(in any) (out GenerationProvider, err error) {
106
	if ret, ok := in.(*v1alpha1.IstioStatus); ok && ret != nil {
107
		return &IstioGenerationProvider{ret}, nil
108
	}
109
	return nil, fmt.Errorf("cannot cast %T: %v to GenerationProvider", in, in)
110
}
111

112
func NewIstioContext(stop <-chan struct{}) context.Context {
113
	ctx, cancel := context.WithCancel(context.Background())
114
	go func() {
115
		<-stop
116
		cancel()
117
	}()
118
	return ctx
119
}
120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.