2017-08-10 03:22:54 +00:00
|
|
|
/*
|
2017-08-17 17:05:01 +00:00
|
|
|
Copyright 2017 The Kubernetes Authors.
|
2017-08-10 03:22:54 +00:00
|
|
|
|
|
|
|
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 alias
|
|
|
|
|
|
|
|
import (
|
2019-08-26 14:58:44 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2019-08-26 14:58:44 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-08-10 03:22:54 +00:00
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2017-11-08 20:58:57 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2017-08-10 03:22:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type alias struct {
|
2017-11-08 20:58:57 +00:00
|
|
|
r resolver.Resolver
|
2017-08-10 03:22:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 17:05:01 +00:00
|
|
|
// NewParser creates a new Alias annotation parser
|
2017-11-08 20:58:57 +00:00
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
|
|
|
return alias{r}
|
2017-08-10 03:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the annotations contained in the ingress rule
|
2017-08-17 17:05:01 +00:00
|
|
|
// used to add an alias to the provided hosts
|
2019-06-09 22:49:59 +00:00
|
|
|
func (a alias) Parse(ing *networking.Ingress) (interface{}, error) {
|
2019-08-26 14:58:44 +00:00
|
|
|
val, err := parser.GetStringAnnotation("server-alias", ing)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases := sets.NewString()
|
|
|
|
for _, alias := range strings.Split(val, ",") {
|
|
|
|
alias = strings.TrimSpace(alias)
|
|
|
|
if len(alias) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !aliases.Has(alias) {
|
|
|
|
aliases.Insert(alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l := aliases.List()
|
|
|
|
sort.Strings(l)
|
|
|
|
|
|
|
|
return l, nil
|
2017-08-28 06:49:13 +00:00
|
|
|
}
|