@@ -2,6 +2,8 @@ package armclient
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "log/slog"
5
7
"os"
6
8
"strings"
7
9
"time"
@@ -11,7 +13,6 @@ import (
11
13
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
12
14
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
13
15
cache "github.com/patrickmn/go-cache"
14
- zap "go.uber.org/zap"
15
16
"k8s.io/apimachinery/pkg/labels"
16
17
17
18
commonAzidentity "github.com/webdevops/go-common/azuresdk/azidentity"
32
33
33
34
cloud cloudconfig.CloudEnvironment
34
35
35
- logger * zap. SugaredLogger
36
+ logger * slog. Logger
36
37
37
38
cache * cache.Cache
38
39
cacheTtl time.Duration
@@ -49,23 +50,23 @@ type (
49
50
)
50
51
51
52
// NewArmClientFromEnvironment creates new Azure SDK ARM client from environment settings
52
- func NewArmClientFromEnvironment (logger * zap. SugaredLogger ) (* ArmClient , error ) {
53
+ func NewArmClientFromEnvironment (logger * slog. Logger ) (* ArmClient , error ) {
53
54
var azureEnvironment string
54
55
55
56
if azureEnvironment = os .Getenv ("AZURE_ENVIRONMENT" ); azureEnvironment == "" {
56
57
logger .Info (`env var AZURE_ENVIRONMENT is not set, assuming "AzurePublicCloud"` )
57
58
azureEnvironment = string (cloudconfig .AzurePublicCloud )
58
59
59
60
if err := os .Setenv ("AZURE_ENVIRONMENT" , azureEnvironment ); err != nil {
60
- logger . Panic (`unable to set AZURE_ENVIRONMENT` )
61
+ return nil , fmt . Errorf (`unable to set AZURE_ENVIRONMENT` )
61
62
}
62
63
}
63
64
64
65
return NewArmClientWithCloudName (azureEnvironment , logger )
65
66
}
66
67
67
68
// NewArmClient creates new Azure SDK ARM client
68
- func NewArmClient (cloudConfig cloudconfig.CloudEnvironment , logger * zap. SugaredLogger ) * ArmClient {
69
+ func NewArmClient (cloudConfig cloudconfig.CloudEnvironment , logger * slog. Logger ) * ArmClient {
69
70
client := & ArmClient {}
70
71
client .cloud = cloudConfig
71
72
@@ -74,7 +75,7 @@ func NewArmClient(cloudConfig cloudconfig.CloudEnvironment, logger *zap.SugaredL
74
75
75
76
client .TagManager = & ArmClientTagManager {
76
77
client : client ,
77
- logger : logger .With (zap .String ("component" , "armClientTagManager" )),
78
+ logger : logger .With (slog .String ("component" , "armClientTagManager" )),
78
79
}
79
80
80
81
client .initCache ()
@@ -84,10 +85,10 @@ func NewArmClient(cloudConfig cloudconfig.CloudEnvironment, logger *zap.SugaredL
84
85
}
85
86
86
87
// NewArmClientWithCloudName creates new Azure SDK ARM client with environment name as string
87
- func NewArmClientWithCloudName (cloudName string , logger * zap. SugaredLogger ) (* ArmClient , error ) {
88
+ func NewArmClientWithCloudName (cloudName string , logger * slog. Logger ) (* ArmClient , error ) {
88
89
cloudConfig , err := cloudconfig .NewCloudConfig (cloudName )
89
90
if err != nil {
90
- logger . Panic ( err . Error ())
91
+ return nil , err
91
92
}
92
93
93
94
return NewArmClient (cloudConfig , logger ), nil
@@ -100,7 +101,7 @@ func (azureClient *ArmClient) initCache() {
100
101
if ttl , err := time .ParseDuration (val ); err == nil {
101
102
cacheTtl = ttl
102
103
} else {
103
- azureClient . logger . Fatalf (`%s is not a valid value, got "%v", expected duration` , EnvVarServiceDiscoveryTtl , val )
104
+ panic ( fmt . Errorf (`%s is not a valid value, got "%v", expected duration` , EnvVarServiceDiscoveryTtl , val ) )
104
105
}
105
106
}
106
107
azureClient .SetCacheTtl (cacheTtl )
@@ -129,7 +130,7 @@ func (azureClient *ArmClient) initServiceDiscovery() {
129
130
if val := os .Getenv (EnvVarServiceDiscoverySubscriptionTagSelector ); val != "" {
130
131
selector , err := labels .Parse (val )
131
132
if err != nil {
132
- azureClient . logger . Panic (err )
133
+ panic (err )
133
134
}
134
135
azureClient .serviceDiscovery .subscriptionTagSelector = & selector
135
136
}
@@ -139,11 +140,11 @@ func (azureClient *ArmClient) initServiceDiscovery() {
139
140
func (azureClient * ArmClient ) LazyConnect () error {
140
141
ctx := context .Background ()
141
142
142
- azureClient .logger .Infof (
143
- `connecting to Azure Environment "%v" (AzureAD:%s ResourceManager:%s) ` ,
144
- azureClient .cloud .Name ,
145
- azureClient .cloud .ActiveDirectoryAuthorityHost ,
146
- azureClient .cloud .Services [cloud .ResourceManager ].Endpoint ,
143
+ azureClient .logger .Info (
144
+ `connecting to Azure` ,
145
+ slog . String ( "azureEnvironment" , string ( azureClient .cloud .Name )) ,
146
+ slog . String ( "AzureAD" , azureClient .cloud .ActiveDirectoryAuthorityHost ) ,
147
+ slog . String ( "ResourceManager" , azureClient .cloud .Services [cloud .ResourceManager ].Endpoint ) ,
147
148
)
148
149
149
150
// try to get token
@@ -154,7 +155,7 @@ func (azureClient *ArmClient) LazyConnect() error {
154
155
}
155
156
156
157
if tokenInfo := commonAzidentity .ParseAccessToken (accessToken ); tokenInfo != nil {
157
- azureClient .logger .With (zap .Any ("client" , tokenInfo .ToMap ())).Infof (`using Azure client: %v ` , tokenInfo .ToString ())
158
+ azureClient .logger .With (slog .Any ("client" , tokenInfo .ToMap ())).Info (`using Azure client` , slog . String ( "token" , tokenInfo .ToString () ))
158
159
} else {
159
160
azureClient .logger .Warn (`unable to get Azure client information, cannot parse accesstoken` )
160
161
}
@@ -176,9 +177,9 @@ func (azureClient *ArmClient) Connect() error {
176
177
return err
177
178
}
178
179
179
- azureClient .logger .Infof ( `found %v Azure Subscriptions` , len (subscriptionList ))
180
+ azureClient .logger .Info ( fmt . Sprintf ( `found %v Azure Subscriptions` , len (subscriptionList ) ))
180
181
for subscriptionId , subscription := range subscriptionList {
181
- azureClient .logger .Debugf ( `found Azure Subscription "%v" (%v)` , subscriptionId , to .String (subscription .DisplayName ))
182
+ azureClient .logger .Debug ( fmt . Sprintf ( `found Azure Subscription "%v" (%v)` , subscriptionId , to .String (subscription .DisplayName ) ))
182
183
}
183
184
184
185
return nil
0 commit comments