/
constructor.go
77 lines (59 loc) · 2.25 KB
/
constructor.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package recorder_vent
import (
"context"
"time"
"github.com/kelseyhightower/envconfig"
"knative.dev/pkg/system"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
ref "k8s.io/client-go/tools/reference"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/logging"
"knative.dev/eventing/test/lib/recordevents"
)
type envConfig struct {
AgentName string `envconfig:"AGENT_NAME" default:"observer-default" required:"true"`
PodName string `envconfig:"POD_NAME" required:"true"`
Port int `envconfig:"PORT" default:"8080" required:"true"`
}
const (
maxRetry = 5
sleepDuration = 5 * time.Second
)
func NewFromEnv(ctx context.Context) recordevents.EventLog {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
logging.FromContext(ctx).Fatal("Failed to process env var: ", err)
}
logging.FromContext(ctx).Infof("Recorder vent environment configuration: %+v", env)
return NewEventLog(ctx, env.AgentName, env.PodName)
}
func NewEventLog(ctx context.Context, agentName string, podName string) recordevents.EventLog {
on, err := kubeclient.Get(ctx).CoreV1().Pods(system.Namespace()).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
logging.FromContext(ctx).Fatal("Error while trying to retrieve the pod", err)
}
logging.FromContext(ctx).Infof("Going to send events to pod '%s' in namespace '%s'", on.Name, on.Namespace)
reference, err := ref.GetReference(scheme.Scheme, on)
if err != nil {
logging.FromContext(ctx).Fatalf("Could not construct reference to: '%#v' due to: '%v'", on, err)
}
return &recorder{
ctx: ctx,
namespace: system.Namespace(),
agentName: agentName,
ref: reference,
}
}