@@ -17,26 +17,25 @@ limitations under the License.
1717package utils
1818
1919import (
20+ "fmt"
2021 stdlog "log"
2122 "os"
2223
2324 "github.com/fluid-cloudnative/fluid/pkg/common"
25+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426)
2527
2628var (
27- ServerlessPlatformKey string = ""
28- ServerlessPlatformVal string = ""
29- disableApplicationController string = ""
29+ // DEPRECATED: the label key for Fluid webhook to determine serverless platform.
30+ // It's replaced by commmon.AnnotationServerlessPlatform.
31+ DeprecatedServerlessPlatformKey string = ""
32+ disableApplicationController string = ""
3033)
3134
3235func init () {
33- if envVal , exists := os .LookupEnv (common .EnvServerlessPlatformKey ); exists {
34- ServerlessPlatformKey = envVal
35- stdlog .Printf ("Found %s value %s, using it as ServerlessPlatformLabelKey" , common .EnvServerlessPlatformKey , envVal )
36- }
37- if envVal , exists := os .LookupEnv (common .EnvServerlessPlatformVal ); exists {
38- ServerlessPlatformVal = envVal
39- stdlog .Printf ("Found %s value %s, using it as ServerlessPlatformLabelValue" , common .EnvServerlessPlatformVal , envVal )
36+ if envVal , exists := os .LookupEnv (common .DeprecatedEnvServerlessPlatformKey ); exists {
37+ DeprecatedServerlessPlatformKey = envVal
38+ stdlog .Printf ("Found %s value %s, using it as ServerlessPlatformLabelKey" , common .DeprecatedEnvServerlessPlatformKey , envVal )
4039 }
4140 if envVal , exists := os .LookupEnv (common .EnvDisableApplicationController ); exists {
4241 disableApplicationController = envVal
@@ -62,7 +61,7 @@ func InjectCacheDirEnabled(infos map[string]string) (match bool) {
6261}
6362
6463func SkipSidecarPostStartInject (infos map [string ]string ) (match bool ) {
65- return matchedValue (infos , common .InjectSidecarPostStart , common .False )
64+ return KeyValueMatched (infos , common .InjectSidecarPostStart , common .False )
6665}
6766
6867func AppContainerPostStartInjectEnabled (infos map [string ]string ) (match bool ) {
@@ -71,62 +70,89 @@ func AppContainerPostStartInjectEnabled(infos map[string]string) (match bool) {
7170
7271// ---- Utils functions to decide serverless platform ----
7372const (
74- PlatformDefault = "Default "
75- PlatformUnprivileged = "Unprivileged "
73+ ServerlessPlatformDefault = "default "
74+ ServerlessPlatformUnprivileged = "unprivileged "
7675)
7776
78- func GetServerlessPlatform (infos map [string ]string ) (platform string ) {
79- if matchedKey (infos , ServerlessPlatformKey ) {
80- return infos [ServerlessPlatformKey ]
77+ func GetServerlessPlatform (metaObj metav1.ObjectMeta ) (platform string , err error ) {
78+ metaLabels := metaObj .Labels
79+ metaAnnotations := metaObj .Annotations
80+
81+ // Setting both DeprecatedServerlessPlatformKey and common.InjectServerless is not allowed
82+ if KeyMatched (metaLabels , DeprecatedServerlessPlatformKey ) && enabled (metaLabels , common .InjectServerless ) {
83+ err = fmt .Errorf ("\" %s\" and \" %s\" is not allowed to set together, remove \" %s\" and retry" , DeprecatedServerlessPlatformKey , common .InjectServerless , DeprecatedServerlessPlatformKey )
84+ return
85+ }
86+
87+ // handle deprecated serverless platform key.
88+ if KeyMatched (metaLabels , DeprecatedServerlessPlatformKey ) {
89+ platform = metaLabels [DeprecatedServerlessPlatformKey ]
90+ return
8191 }
8292
83- if enabled (infos , common .InjectServerless ) || enabled (infos , common .InjectFuseSidecar ) {
84- if enabled (infos , common .InjectUnprivilegedFuseSidecar ) {
85- return PlatformUnprivileged
93+ // handle deprecated common.InjectFuseSidecar. In this case,
94+ // only two platforms are supported: PlatformDefault and PlatformUnprivileged
95+ if enabled (metaLabels , common .InjectFuseSidecar ) {
96+ if enabled (metaLabels , common .InjectUnprivilegedFuseSidecar ) {
97+ platform = ServerlessPlatformUnprivileged
8698 } else {
87- return PlatformDefault
99+ platform = ServerlessPlatformDefault
100+ }
101+ return
102+ }
103+
104+ if enabled (metaLabels , common .InjectServerless ) {
105+ if enabled (metaLabels , common .InjectUnprivilegedFuseSidecar ) {
106+ platform = ServerlessPlatformUnprivileged
107+ return
108+ }
109+
110+ // Setting common.InjectServerless in labels and common.AnnotationServerlessPlatform in annotations
111+ // together to indicate the serverless platform
112+ if KeyMatched (metaAnnotations , common .AnnotationServerlessPlatform ) {
113+ platform = metaAnnotations [common .AnnotationServerlessPlatform ]
114+ return
88115 }
116+
117+ platform = ServerlessPlatformDefault
118+ return
89119 }
90120
91121 // default to an empty platform, meaning no platform is found
92- return ""
122+ return "" , fmt . Errorf ( "no serverless platform can be found from Pod's metadata" )
93123}
94124
95125// ServerlessEnabled decides if fuse sidecar should be injected, whether privileged or unprivileged
96- // - serverlessPlatform implies injecting unprivileged fuse sidecar
97- // - serverless.fluid.io/inject=true implies injecting (privileged/unprivileged) fuse sidecar,
126+ // We don't have to know which serverless platform it is using here.
127+ // - serverless.fluid.io/inject=true implies injecting fuse sidecar.
128+ // - [deprecated] serverlessPlatform implies injecting fuse sidecar according to the deprecated env variable. It's deprecated by common.AnnotationServerlessPlatform.
98129// - [deprecated] fuse.sidecar.fluid.io/inject=true is the deprecated version of serverless.fluid.io/inject=true
99130func ServerlessEnabled (infos map [string ]string ) (match bool ) {
100- return serverlessPlatformMatched (infos ) || enabled (infos , common .InjectServerless ) || enabled (infos , common .InjectFuseSidecar )
101- }
102-
103- // FuseSidecarUnprivileged decides if the injected fuse sidecar should be unprivileged, only used when fuse sidecar should be injected
104- // - serverlessPlatform implies injecting unprivileged fuse sidecar
105- // - serverless.fluid.io/inject=true + unprivileged.sidecar.fluid.io/inject=true implies injecting unprivileged fuse sidecar,
106- func FuseSidecarUnprivileged (infos map [string ]string ) (match bool ) {
107- return serverlessPlatformMatched (infos ) || (ServerlessEnabled (infos ) && enabled (infos , common .InjectUnprivilegedFuseSidecar ))
131+ return enabled (infos , common .InjectServerless ) || serverlessPlatformMatched (infos ) || enabled (infos , common .InjectFuseSidecar )
108132}
109133
110134// FuseSidecarPrivileged decides if the injected fuse sidecar should be privileged, only used when fuse sidecar should be injected
111- // - sidecar is privileged only when setting serverless.fluid.io/inject=true without unprivileged.sidecar.fluid.io/inject=true
112- func FuseSidecarPrivileged (infos map [string ]string ) (match bool ) {
113- return enabled (infos , common .InjectServerless ) && ! (enabled (infos , common .InjectUnprivilegedFuseSidecar ))
135+ // TODO: The func is used for Fluid App controller to determine if it's a pod should be watched. It could be better to use another way(e.g. a special label)to indicate this.
136+ func FuseSidecarPrivileged (metaObj metav1.ObjectMeta ) (match bool ) {
137+ // error can be ignored here because platform equals to "" when error is not nil
138+ platform , _ := GetServerlessPlatform (metaObj )
139+ return platform == ServerlessPlatformDefault
114140}
115141
116142func InjectSidecarDone (infos map [string ]string ) (match bool ) {
117143 return enabled (infos , common .InjectSidecarDone )
118144}
119145
120146func AppControllerDisabled (info map [string ]string ) (match bool ) {
121- return matchedKey (info , disableApplicationController )
147+ return KeyMatched (info , disableApplicationController )
122148}
123149
124150func serverlessPlatformMatched (infos map [string ]string ) (match bool ) {
125- if len (ServerlessPlatformKey ) == 0 {
151+ if len (DeprecatedServerlessPlatformKey ) == 0 {
126152 return
127153 }
128154
129- return matchedKey (infos , ServerlessPlatformKey )
155+ return KeyMatched (infos , DeprecatedServerlessPlatformKey )
130156}
131157
132158func SkipPrecheckEnable (infos map [string ]string ) (match bool ) {
@@ -135,27 +161,5 @@ func SkipPrecheckEnable(infos map[string]string) (match bool) {
135161
136162// enabled checks if the given name has a value of "true"
137163func enabled (infos map [string ]string , name string ) (match bool ) {
138- return matchedValue (infos , name , common .True )
139- }
140-
141- // matchedValue checks if the given name has the expected value
142- func matchedValue (infos map [string ]string , name string , val string ) (match bool ) {
143- for key , value := range infos {
144- if key == name && value == val {
145- match = true
146- break
147- }
148- }
149- return
150- }
151-
152- // matchedKey checks if the given name exists
153- func matchedKey (infos map [string ]string , name string ) (match bool ) {
154- for key := range infos {
155- if key == name {
156- match = true
157- break
158- }
159- }
160- return
164+ return KeyValueMatched (infos , name , common .True )
161165}
0 commit comments