-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add fetchers.go to fetch EnvVar actual value
- Loading branch information
Showing
5 changed files
with
125 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package seata | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
seatav1alpha1 "github.com/apache/seata-k8s/api/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func FetchEnvVar(ctx context.Context, c client.Client, cr *seatav1alpha1.SeataServer, envVar v1.EnvVar) (string, error) { | ||
if envVar.ValueFrom == nil { | ||
return envVar.Value, nil | ||
} | ||
|
||
// Inspired by kubelet#makeEnvironmentVariables, determine the final values of variables. | ||
// See https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/kubelet_pods.go#L694-L806 | ||
var result string | ||
switch { | ||
case envVar.ValueFrom.ConfigMapKeyRef != nil: | ||
cm := envVar.ValueFrom.ConfigMapKeyRef | ||
name := cm.Name | ||
key := cm.Key | ||
optional := cm.Optional != nil && *cm.Optional | ||
|
||
configMap := &v1.ConfigMap{} | ||
err := c.Get(ctx, types.NamespacedName{Name: name, Namespace: cr.Namespace}, configMap) | ||
if err != nil { | ||
if errors.IsNotFound(err) && optional { | ||
// ignore error when marked optional | ||
return result, nil | ||
} | ||
return result, err | ||
} | ||
runtimeVal, ok := configMap.Data[key] | ||
if !ok { | ||
if optional { | ||
return result, nil | ||
} | ||
return result, fmt.Errorf("couldn't find key %v in ConfigMap %v/%v", key, cr.Namespace, name) | ||
} | ||
result = runtimeVal | ||
case envVar.ValueFrom.SecretKeyRef != nil: | ||
s := envVar.ValueFrom.SecretKeyRef | ||
name := s.Name | ||
key := s.Key | ||
optional := s.Optional != nil && *s.Optional | ||
secret := &v1.Secret{} | ||
err := c.Get(ctx, types.NamespacedName{Name: name, Namespace: cr.Namespace}, secret) | ||
if err != nil { | ||
if errors.IsNotFound(err) && optional { | ||
// ignore error when marked optional | ||
return result, nil | ||
} | ||
return result, err | ||
} | ||
runtimeValBytes, ok := secret.Data[key] | ||
if !ok { | ||
if optional { | ||
return result, nil | ||
} | ||
return result, fmt.Errorf("couldn't find key %v in Secret %v/%v", key, cr.Namespace, name) | ||
} | ||
runtimeVal := string(runtimeValBytes) | ||
result = runtimeVal | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters