Skip to content

Commit

Permalink
Update projects files for SDL check, and fix warnings. (Azure#1418)
Browse files Browse the repository at this point in the history
* Update projects files for SDL check, and fix warnings.
  • Loading branch information
darobs authored Jul 8, 2019
1 parent 85346e7 commit 3c2e0b4
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes
{
public static class Constants
{
public const string k8sApi = "microsoft.azure.devices.edge";
public const string K8sApi = "microsoft.azure.devices.edge";

public const string k8sApiVersion = "v1beta1";
public const string K8sApiVersion = "v1beta1";

public const string k8sCrdKind = "EdgeDeployment";
public const string K8sCrdKind = "EdgeDeployment";

public const string k8sCrdGroup = "microsoft.azure.devices.edge";
public const string K8sCrdGroup = "microsoft.azure.devices.edge";

public const string k8sCrdPlural = "edgedeployments";
public const string K8sCrdPlural = "edgedeployments";

public const string k8sNamespace = "microsoft-azure-devices-edge";
public const string K8sNamespace = "microsoft-azure-devices-edge";

public const string k8sNamespaceBaseName = "K8sNamespaceBaseName";
public const string K8sNamespaceBaseName = "K8sNamespaceBaseName";

public const string k8sEdgeModuleLabel = "net.azure-devices.edge.module";
public const string K8sEdgeModuleLabel = "net.azure-devices.edge.module";

public const string k8sEdgeOriginalModuleId = "net.azure-devices.edge.original-moduleid";
public const string K8sEdgeOriginalModuleId = "net.azure-devices.edge.original-moduleid";

public const string k8sEdgeDeviceLabel = "net.azure-devices.edge.deviceid";
public const string K8sEdgeDeviceLabel = "net.azure-devices.edge.deviceid";

public const string k8sEdgeHubNameLabel = "net.azure-devices.edge.hub";
public const string K8sEdgeHubNameLabel = "net.azure-devices.edge.hub";

public const string CreationString = "net.azure-devices.edge.creationstring";

public const string k8sNameDivider = "-";
public const string K8sNameDivider = "-";

public const string k8sPullSecretType = "kubernetes.io/dockerconfigjson";
public const string K8sPullSecretType = "kubernetes.io/dockerconfigjson";

public const string k8sPullSecretData = ".dockerconfigjson";
public const string K8sPullSecretData = ".dockerconfigjson";

public const string proxyImage = "darobs/envoy:0.1";
public const string ProxyImage = "darobs/envoy:0.1";

public const string AgentConfigMap = "edgeagentconfigmap";

public const string ModuleConfigMap = "moduleconfigmap";

public const PortMapServiceType DefaultPortMapServiceType = PortMapServiceType.ClusterIP;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class EdgeDeploymentDefinition<TConfig> : IEdgeDeploymentDefinition<TConfig>
[JsonProperty(PropertyName = "kind")]
public string Kind { get; set; }


[JsonProperty(PropertyName = "metadata")]
public V1ObjectMeta Metadata { get; }

Expand All @@ -25,10 +24,10 @@ class EdgeDeploymentDefinition<TConfig> : IEdgeDeploymentDefinition<TConfig>

public EdgeDeploymentDefinition(string apiVersion, string kind, V1ObjectMeta metadata, IList<KubernetesModule<TConfig>> spec)
{
ApiVersion = Preconditions.CheckNonWhiteSpace(apiVersion, nameof(apiVersion));
Kind = Preconditions.CheckNonWhiteSpace(kind, nameof(kind));
Metadata = Preconditions.CheckNotNull(metadata, nameof(metadata));
Spec = Preconditions.CheckNotNull(spec, nameof(spec));
this.ApiVersion = Preconditions.CheckNonWhiteSpace(apiVersion, nameof(apiVersion));
this.Kind = Preconditions.CheckNonWhiteSpace(kind, nameof(kind));
this.Metadata = Preconditions.CheckNotNull(metadata, nameof(metadata));
this.Spec = Preconditions.CheckNotNull(spec, nameof(spec));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes
interface IEdgeDeploymentDefinition<TConfig> : IKubernetesObject
{
[JsonProperty(PropertyName = "metadata")]
V1ObjectMeta Metadata { get;}
V1ObjectMeta Metadata { get; }

[JsonProperty(PropertyName = "spec")]
IList<KubernetesModule<TConfig>> Spec { get;}
IList<KubernetesModule<TConfig>> Spec { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes
[Serializable]
public class InvalidIdentityException : Exception
{
public InvalidIdentityException(string message) : base(message)
public InvalidIdentityException(string message)
: base(message)
{

}
public InvalidIdentityException(string message, Exception inner) : base(message, inner)
{

public InvalidIdentityException(string message, Exception inner)
: base(message, inner)
{
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes
[Serializable]
public class InvalidKubernetesNameException : Exception
{
public InvalidKubernetesNameException(string message) : base(message)
public InvalidKubernetesNameException(string message)
: base(message)
{

}
public InvalidKubernetesNameException(string message, Exception inner) : base(message, inner)
{

public InvalidKubernetesNameException(string message, Exception inner)
: base(message, inner)
{
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public static class KubeUtils
const int MaxDnsNameLength = 63;
const int MaxLabelValueLength = 63;
static readonly HashSet<char> AlphaHashSet = new HashSet<char>(Alphabet.ToCharArray());
static readonly HashSet<char> AlphaNumericHashSet = new HashSet<char>( (Alphabet+Numeric).ToCharArray());
static readonly HashSet<char> AllowedLabelsHashSet = new HashSet<char>((Alphabet+Numeric+AllowedCharsLabelValues).ToCharArray());
static readonly HashSet<char> AllowedDnsHashSet = new HashSet<char>((Alphabet+Numeric+AllowedCharsDns).ToCharArray());
static readonly HashSet<char> AllowedGenericHashSet = new HashSet<char>((Alphabet+Numeric+AllowedCharsGeneric).ToCharArray());
static readonly HashSet<char> AlphaNumericHashSet = new HashSet<char>((Alphabet + Numeric).ToCharArray());
static readonly HashSet<char> AllowedLabelsHashSet = new HashSet<char>((Alphabet + Numeric + AllowedCharsLabelValues).ToCharArray());
static readonly HashSet<char> AllowedDnsHashSet = new HashSet<char>((Alphabet + Numeric + AllowedCharsDns).ToCharArray());
static readonly HashSet<char> AllowedGenericHashSet = new HashSet<char>((Alphabet + Numeric + AllowedCharsGeneric).ToCharArray());

static bool IsAlpha(char ch) => AlphaHashSet.Contains(ch);

static bool IsAlphaNumeric(char ch) => AlphaNumericHashSet.Contains(ch);

// Valid annotation keys have two segments: an optional prefix and name, separated by a slash (/).
// The name segment is required and must be 63 characters or less, beginning and ending with an
// alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between.
// Valid annotation keys have two segments: an optional prefix and name, separated by a slash (/).
// The name segment is required and must be 63 characters or less, beginning and ending with an
// alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between.
// The prefix is optional. If specified, the prefix must be a DNS subdomain
public static string SanitizeAnnotationKey(string key)
{
Expand Down Expand Up @@ -130,10 +130,12 @@ public static string SanitizeDNSValue(string name)
output.Append(name[i]);
}
}

if (output.Length > MaxDnsNameLength)
{
throw new InvalidKubernetesNameException($"DNS name '{name}' exceeded maximum length of {MaxDnsNameLength}");
}

if (output.Length == 0)
{
throw new InvalidKubernetesNameException($"DNS name '{name}' as sanitized is empty");
Expand Down Expand Up @@ -192,6 +194,7 @@ private static string SanitizeNameValue(string name)
{
start++;
}

if (start == name.Length)
{
throw new InvalidKubernetesNameException($"Name '{name}' does not start with a valid character");
Expand All @@ -214,6 +217,7 @@ private static string SanitizeNameValue(string name)
output.Append(name[i]);
}
}

if (output.Length > MaxLabelValueLength)
{
throw new InvalidKubernetesNameException($"Name '{name}' exceeded maximum length of {MaxLabelValueLength}");
Expand All @@ -238,6 +242,7 @@ public static string SanitizeLabelValue(string name)
{
throw new InvalidKubernetesNameException($"Name '{name}' is null or empty");
}

return SanitizeNameValue(name.ToLower());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes
[Serializable]
public class KuberenetesResponseException : Exception
{
public KuberenetesResponseException(string message) : base(message)
public KuberenetesResponseException(string message)
: base(message)
{

}
public KuberenetesResponseException(string message, Exception inner) : base(message, inner)
{

public KuberenetesResponseException(string message, Exception inner)
: base(message, inner)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes
using k8s;
using k8s.Models;
using Microsoft.Azure.Devices.Edge.Agent.Core;
using AgentDocker = Microsoft.Azure.Devices.Edge.Agent.Docker;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Azure.Devices.Edge.Util.Concurrency;
using Microsoft.Extensions.Logging;
using Microsoft.Rest;

using AgentDocker = Microsoft.Azure.Devices.Edge.Agent.Docker;

public class KubernetesRuntimeInfoProvider : IKubernetesOperator, IRuntimeInfoProvider, INotifyPropertyChanged
{
readonly string deviceNamespace;
readonly IKubernetes client;
Option<Watcher<V1Pod>> podWatch;
readonly Dictionary<string, ModuleRuntimeInfo> moduleRuntimeInfos;
readonly AsyncLock moduleLock;
Option<Watcher<V1Pod>> podWatch;

public KubernetesRuntimeInfoProvider(string deviceNamespace, IKubernetes client)
{
this.deviceNamespace = Preconditions.CheckNonWhiteSpace(deviceNamespace,nameof(deviceNamespace));
this.deviceNamespace = Preconditions.CheckNonWhiteSpace(deviceNamespace, nameof(deviceNamespace));
this.client = Preconditions.CheckNotNull(client, nameof(client));

this.podWatch = Option.None<Watcher<V1Pod>>();
Expand Down Expand Up @@ -65,10 +65,9 @@ private async Task ListPodComplete(Task<HttpOperationResponse<V1PodList>> podLis
this.podWatch = Option.None<Watcher<V1Pod>>();

// kick off a new watch
this.client.ListNamespacedPodWithHttpMessagesAsync(deviceNamespace, watch: true).ContinueWith(this.ListPodComplete);
this.client.ListNamespacedPodWithHttpMessagesAsync(this.deviceNamespace, watch: true).ContinueWith(this.ListPodComplete);
},
onError: Events.ExceptionInPodWatch
));
onError: Events.ExceptionInPodWatch));
}
else
{
Expand All @@ -83,8 +82,7 @@ private async Task ListPodComplete(Task<HttpOperationResponse<V1PodList>> podLis
}
}


public void Start() => this.client.ListNamespacedPodWithHttpMessagesAsync(deviceNamespace, watch: true).ContinueWith(this.ListPodComplete);
public void Start() => this.client.ListNamespacedPodWithHttpMessagesAsync(this.deviceNamespace, watch: true).ContinueWith(this.ListPodComplete);

public Task CloseAsync(CancellationToken token) => Task.CompletedTask;

Expand Down Expand Up @@ -128,12 +126,11 @@ public async Task<SystemInfo> GetSystemInfo()
async Task WatchPodEventsAsync(WatchEventType type, V1Pod item)
{
// if the pod doesn't have the module label set then we are not interested in it
if (! item.Metadata.Labels.ContainsKey(Constants.k8sEdgeModuleLabel))
if (!item.Metadata.Labels.TryGetValue(Constants.K8sEdgeModuleLabel, out string podName))
{
return;
}

string podName = item.Metadata.Labels[Constants.k8sEdgeModuleLabel];
Events.PodStatus(type, podName);
switch (type)
{
Expand All @@ -158,11 +155,9 @@ async Task WatchPodEventsAsync(WatchEventType type, V1Pod item)
}

this.OnModulesChanged();

}

break;

}
}

Expand Down Expand Up @@ -196,9 +191,9 @@ static Option<V1ContainerStatus> GetContainerByName(string name, V1Pod pod)
{
string containerName = KubeUtils.SanitizeDNSValue(name);
return pod.Status?.ContainerStatuses
.Where(status => string.Equals(status.Name, containerName, StringComparison.OrdinalIgnoreCase))
.Select(status => Option.Some(status))
.FirstOrDefault() ?? Option.None<V1ContainerStatus>();
.Where(status => string.Equals(status.Name, containerName, StringComparison.OrdinalIgnoreCase))
.Select(status => Option.Some(status))
.FirstOrDefault() ?? Option.None<V1ContainerStatus>();
}

static RuntimeData GetTerminatedRuntimedata(V1ContainerStateTerminated term, string imageName)
Expand Down Expand Up @@ -242,21 +237,24 @@ static RuntimeData GetRuntimedata(V1ContainerStatus status)
ModuleRuntimeInfo ConvertPodToRuntime(string name, V1Pod pod)
{
Option<V1ContainerStatus> containerStatus = GetContainerByName(name, pod);
ReportedModuleStatus moduleStatus = this.ConvertPodStatusToModuleStatus(containerStatus);
ReportedModuleStatus moduleStatus = this.ConvertPodStatusToModuleStatus(containerStatus);
RuntimeData runtimeData = GetRuntimedata(containerStatus.OrDefault());

string moduleName = name;
pod.Metadata?.Annotations?.TryGetValue(Constants.k8sEdgeOriginalModuleId, out moduleName);
string moduleName = string.Empty;
if (!(pod.Metadata?.Annotations?.TryGetValue(Constants.K8sEdgeOriginalModuleId, out moduleName) ?? false))
{
moduleName = name;
}

var reportedConfig = new AgentDocker.DockerReportedConfig(runtimeData.imageName, string.Empty, string.Empty);
var reportedConfig = new AgentDocker.DockerReportedConfig(runtimeData.ImageName, string.Empty, string.Empty);
return new ModuleRuntimeInfo<AgentDocker.DockerReportedConfig>(
ModuleIdentityHelper.GetModuleName(moduleName),
"docker",
moduleStatus.status,
moduleStatus.description,
runtimeData.exitStatus,
runtimeData.startTime,
runtimeData.endTime,
moduleStatus.Status,
moduleStatus.Description,
runtimeData.ExitStatus,
runtimeData.StartTime,
runtimeData.EndTime,
reportedConfig);
}

Expand All @@ -269,36 +267,36 @@ protected virtual void OnModulesChanged()

class ReportedModuleStatus
{
public readonly ModuleStatus status;
public readonly string description;
public readonly ModuleStatus Status;
public readonly string Description;

public ReportedModuleStatus(ModuleStatus status, string description)
{
this.status = status;
this.description = description;
this.Status = status;
this.Description = description;
}
}

class RuntimeData
{
public readonly int exitStatus;
public readonly Option<DateTime> startTime;
public readonly Option<DateTime> endTime;
public readonly string imageName;
public readonly int ExitStatus;
public readonly Option<DateTime> StartTime;
public readonly Option<DateTime> EndTime;
public readonly string ImageName;

public RuntimeData(int exitStatus, Option<DateTime> startTime, Option<DateTime> endTime, string image)
{
this.exitStatus = exitStatus;
this.startTime = startTime;
this.endTime = endTime;
this.imageName = image;
this.ExitStatus = exitStatus;
this.StartTime = startTime;
this.EndTime = endTime;
this.ImageName = image;
}
}

static class Events
{
static readonly ILogger Log = Logger.Factory.CreateLogger<KubernetesRuntimeInfoProvider>();
const int IdStart = KubernetesEventIds.KubernetesReporter;
static readonly ILogger Log = Logger.Factory.CreateLogger<KubernetesRuntimeInfoProvider>();

enum EventIds
{
Expand Down
Loading

0 comments on commit 3c2e0b4

Please sign in to comment.