Skip to content

Instantly share code, notes, and snippets.

@s1061123
Created December 4, 2019 09:35
Show Gist options
  • Save s1061123/cf0f651826be7660e4dcb52ca65bc3c5 to your computer and use it in GitHub Desktop.
Save s1061123/cf0f651826be7660e4dcb52ca65bc3c5 to your computer and use it in GitHub Desktop.
diff --git a/pkg/controller/endpoint/endpoints_controller.go b/pkg/controller/endpoint/endpoints_controller.go
index 6df6565..357dc1c 100644
--- a/pkg/controller/endpoint/endpoints_controller.go
+++ b/pkg/controller/endpoint/endpoints_controller.go
@@ -17,6 +17,7 @@ limitations under the License.
package endpoint
import (
+ "encoding/json"
"fmt"
"reflect"
"strconv"
@@ -461,8 +462,54 @@ func (e *EndpointController) syncService(key string) error {
totalNotReadyEps = totalNotReadyEps + notReadyEps
}
}
+ //XXX: Add net-attach-def IP into subset
+ networkStatus, err := GetNetworkStatus(pod)
+ if len(networkStatus) != 0 {
+ fmt.Printf("XXX: Found!\n")
+ for _, v := range networkStatus {
+ if v.Name == "eth0" {
+ continue
+ }
+ for _, ip := range v.IPs {
+ fmt.Printf("\t XXX: add endpoint: %s, %s %v %v\n", v.Name, v.Interface, v.IPs, v.Default)
+ epa2 := v1.EndpointAddress{
+ IP: ip,
+ NodeName: &pod.Spec.NodeName,
+ TargetRef: &v1.ObjectReference{
+ Kind: "Pod",
+ Namespace: pod.ObjectMeta.Namespace,
+ Name: pod.ObjectMeta.Name,
+ UID: pod.ObjectMeta.UID,
+ ResourceVersion: pod.ObjectMeta.ResourceVersion,
+ }}
+
+ for i := range service.Spec.Ports {
+ servicePort := &service.Spec.Ports[i]
+
+ portName := servicePort.Name
+ portProto := servicePort.Protocol
+ portNum, err := podutil.FindPort(pod, servicePort)
+ if err != nil {
+ klog.V(4).Infof("Failed to find port for service %s/%s: %v", service.Namespace, service.Name, err)
+ continue
+ }
+
+ var readyEps, notReadyEps int
+ epp2 := &v1.EndpointPort{Name: portName, Port: int32(portNum), Protocol: portProto}
+ subsets, readyEps, notReadyEps = addEndpointSubset(subsets, pod, epa2, epp2, tolerateUnreadyEndpoints)
+ totalReadyEps = totalReadyEps + readyEps
+ totalNotReadyEps = totalNotReadyEps + notReadyEps
+ }
+ }
+ }
+ } else {
+ fmt.Printf("XXX: Not Found!\n")
+ }
+
}
+ fmt.Printf("XXX1: %v\n", subsets)
subsets = endpoints.RepackSubsets(subsets)
+ fmt.Printf("XXX2: %v\n", subsets)
// See if there's actually an update here.
currentEndpoints, err := e.endpointsLister.Endpoints(service.Namespace).Get(service.Name)
@@ -604,3 +651,47 @@ func shouldPodBeInEndpoints(pod *v1.Pod) bool {
return true
}
}
+
+// NetworkStatus is for network status annotation for pod
+type NetworkStatus struct {
+ Name string `json:"name"`
+ Interface string `json:"interface,omitempty"`
+ IPs []string `json:"ips,omitempty"`
+ Mac string `json:"mac,omitempty"`
+ Default bool `json:"default,omitempty"`
+ DNS DNS `json:"dns,omitempty"`
+}
+
+type DNS struct {
+ Nameservers []string `json:"nameservers,omitempty"`
+ Domain string `json:"domain,omitempty"`
+ Search []string `json:"search,omitempty"`
+ Options []string `json:"options,omitempty"`
+}
+
+const (
+ // Pod annotation for network-attachment-definition
+ NetworkAttachmentAnnot = "k8s.v1.cni.cncf.io/networks"
+ // Pod annotation for network status
+ NetworkStatusAnnot = "k8s.v1.cni.cncf.io/networks-status"
+)
+
+// GetNetworkStatus returns pod's network status
+func GetNetworkStatus(pod *v1.Pod) ([]NetworkStatus, error) {
+ if pod == nil {
+ return nil, fmt.Errorf("cannot find pod")
+ }
+ if pod.Annotations == nil {
+ return nil, fmt.Errorf("cannot find pod annotation")
+ }
+
+ netStatusesJson, ok := pod.Annotations[NetworkStatusAnnot]
+ if !ok {
+ return nil, fmt.Errorf("cannot find network status")
+ }
+
+ var netStatuses []NetworkStatus
+ err := json.Unmarshal([]byte(netStatusesJson), &netStatuses)
+
+ return netStatuses, err
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment