func getStatuses()

in controllers/autoneg.go [466:601]


func getStatuses(namespace string, name string, annotations map[string]string, r *ServiceReconciler) (s Statuses, valid bool, err error) {
	// Read the current cloud.google.com/neg annotation
	tmp, ok := annotations[negAnnotation]
	if ok {
		// Found a status, decode
		if err = json.Unmarshal([]byte(tmp), &s.negConfig); err != nil {
			return
		}
	}

	// Is this service using autoneg in new mode?
	oldOk := false
	tmp, newOk := annotations[autonegAnnotation]
	if newOk {
		valid = true

		var tempConfig AutonegConfigTemp
		if err = json.Unmarshal([]byte(tmp), &tempConfig); err != nil {
			return
		}

		tmpSync, syncOk := annotations[autonegSyncAnnotation]
		if syncOk {
			if err = json.Unmarshal([]byte(tmpSync), &s.syncConfig); err != nil {
				return
			}
		}

		s.config.BackendServices = make(map[string]map[string]AutonegNEGConfig, len(tempConfig.BackendServices))
		for port, cfgs := range tempConfig.BackendServices {
			s.config.BackendServices[port] = make(map[string]AutonegNEGConfig, len(cfgs))
			for _, cfg := range cfgs {
				if cfg.Name == "" || !r.AllowServiceName {
					// Default to name generated using serviceNameTemplate
					cfg.Name = generateServiceName(namespace, name, port, r.ServiceNameTemplate)
				}

				//Use defaults if rate and connections have not been set
				if cfg.Rate == 0 && cfg.Connections == 0 {
					if r.MaxRatePerEndpointDefault > 0 {
						cfg.Rate = r.MaxRatePerEndpointDefault
					} else {
						cfg.Connections = r.MaxConnectionsPerEndpointDefault
					}
				}

				s.config.BackendServices[port][cfg.Name] = cfg
			}
		}

		// Is this autoneg config valid?
		if err = validateNewConfig(s.config); err != nil {
			return
		}

		s.newConfig = true
	}

	// Does service has new auto neg status?
	tmp, statusOk := annotations[autonegStatusAnnotation]
	if statusOk {
		// Status annotation found but auto neg annotation not found set empty auto neg config
		if !newOk && r.DeregisterNEGsOnAnnotationRemoval {
			s.config = AutonegConfig{}
			valid = true
			s.newConfig = true
		}
		// Found a status, decode
		if err = json.Unmarshal([]byte(tmp), &s.status); err != nil {
			return
		}
	}

	if !newOk && !statusOk {
		// Is this service using autoneg in legacy mode?
		tmp, oldOk = annotations[oldAutonegAnnotation]
		if oldOk {
			valid = true

			if err = json.Unmarshal([]byte(tmp), &s.oldConfig); err != nil {
				return
			}

			// Default to the k8s service name
			if s.oldConfig.Name == "" {
				s.oldConfig.Name = name
			}

			// Is this autoneg config valid?
			if err = validateOldConfig(s.oldConfig); err != nil {
				return
			}

			// Convert the old configuration to a new style configuration
			s.config.BackendServices = make(map[string]map[string]AutonegNEGConfig, 1)
			if len(s.negConfig.ExposedPorts) == 1 {
				var firstPort string
				for k, _ := range s.negConfig.ExposedPorts {
					firstPort = k
					break
				}
				s.config.BackendServices[firstPort] = make(map[string]AutonegNEGConfig, 1)
				s.config.BackendServices[firstPort][s.oldConfig.Name] = AutonegNEGConfig{
					Name:        s.oldConfig.Name,
					Rate:        s.oldConfig.Rate,
					Connections: 0,
				}
			} else {
				err = errors.New(fmt.Sprintf("more than one port in %s, but autoneg configuration is for one or no ports", negAnnotation))
				return
			}
		}

		tmp, ok = annotations[oldAutonegStatusAnnotation]
		if ok {
			// Status annotation found but auto neg annotation not found set empty auto neg config
			if !oldOk && r.DeregisterNEGsOnAnnotationRemoval {
				s.oldConfig = OldAutonegConfig{}
				valid = true
			}
			// Found a status, decode
			if err = json.Unmarshal([]byte(tmp), &s.oldStatus); err != nil {
				return
			}
		}
	}

	tmp, ok = annotations[negStatusAnnotation]
	if ok {
		// Found a status, decode
		if err = json.Unmarshal([]byte(tmp), &s.negStatus); err != nil {
			return
		}
	}
	return
}