/
main.go
105 lines (91 loc) · 3 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright (c) Microsoft Corporation.
Licensed under the Apache 2.0 license.
*/
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/sirupsen/logrus"
"github.com/virtual-kubelet/azure-aci/pkg/auth"
"github.com/virtual-kubelet/azure-aci/pkg/network"
"github.com/virtual-kubelet/azure-aci/pkg/util"
"github.com/virtual-kubelet/virtual-kubelet/log"
logruslogger "github.com/virtual-kubelet/virtual-kubelet/log/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/retry"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
logger := logrus.StandardLogger()
log.L = logruslogger.FromLogrus(logrus.NewEntry(logger))
log.G(ctx).Debug("Init container started")
podName := os.Getenv("POD_NAME")
podNamespace := os.Getenv("NAMESPACE")
if podName == "" || podNamespace == "" {
log.G(ctx).Fatal("an error has occurred while retrieve the pod info ")
}
config, err := clientcmd.BuildConfigFromFlags("", "")
if err != nil {
log.G(ctx).Fatal("an error has occurred while creating client ", err)
}
kubeClient := kubernetes.NewForConfigOrDie(config)
eventBroadcast := util.NewRecorder(ctx, kubeClient)
defer eventBroadcast.Shutdown()
recorder := eventBroadcast.NewRecorder(scheme.Scheme, v1.EventSource{Component: "virtual kubelet"})
setupBackoff := wait.Backoff{
Steps: 50,
Duration: time.Minute,
Factor: 0,
Jitter: 0.01,
}
azConfig := auth.Config{}
//Setup config
err = azConfig.SetAuthConfig(ctx)
if err != nil {
log.G(ctx).Fatalf("cannot setup the auth configuration. Retrying, ", err)
}
err = retry.OnError(setupBackoff,
func(err error) bool {
return true
}, func() error {
var providerNetwork network.ProviderNetwork
if azConfig.AKSCredential != nil {
providerNetwork.VnetName = azConfig.AKSCredential.VNetName
if azConfig.AKSCredential.VNetResourceGroup != "" {
providerNetwork.VnetResourceGroup = azConfig.AKSCredential.VNetResourceGroup
} else {
providerNetwork.VnetResourceGroup = azConfig.AKSCredential.ResourceGroup
}
}
// Check or set up a network for VK
log.G(ctx).Debug("setting up the network configuration")
err = providerNetwork.SetVNETConfig(ctx, &azConfig)
if err != nil {
log.G(ctx).Errorf("cannot setup the VNet configuration. Retrying", err)
return err
}
return nil
})
if err != nil {
recorder.Eventf(&v1.ObjectReference{
Kind: "Pod",
Name: podName,
Namespace: podNamespace,
}, v1.EventTypeWarning, "InitFailed", "VNet config setup failed")
log.G(ctx).Fatal("cannot setup the VNet configuration ", err)
}
recorder.Eventf(&v1.ObjectReference{
Kind: "Pod",
Name: podName,
Namespace: podNamespace,
}, v1.EventTypeNormal, "InitSuccess", "initial setup for virtual kubelet Azure ACI is successful")
log.G(ctx).Info("initial setup for virtual kubelet Azure ACI is successful")
}